Anda di halaman 1dari 9

Public Sub doADO()

On Error GoTo doADOErr



Dim myData As New ADODB.Recordset
Dim conn As New ADODB.Connection

Set conn = CurrentProject.Connection
myData.Open "SELECT * FROM tblName", conn, adOpenForwardOnly, adLockReadOnly
If Not myData.EOF Then
With myData
Do Until .EOF
txtField = !DataBaseField1
.MoveNext
Loop
End With
End If
myData.Close
conn.Close

doADOExit:
Exit Sub

doADO:
MsgBox Err.Description, vbOKOnly, Err.Source & ":" & Err.Number
Resume doADOExit
End Sub
Sub ConnectToDatabase()
Dim Connection As ADODB.Connection
On Error GoTo Except
Set Connection = New ADODB.Connection
Connection.Provider = "Microsoft.Jet.OLEDB.4.0"
Connection.ConnectionString = "data source=C:\nwind.m
db"
Connection.Mode = adModeReadWrite
Connection.Open
MsgBox "Connected via " & Connection.Provider & " OLE
DB Provider!", vbInformation
Exit Sub
Except:
MsgBox Err.Description, vbCritical
End Sub
Sub CreateConnection()
'Declare and instantiate the connection
Dim cnn As ADODB.Connection
Set cnn = New ADODB.Connection

'Open the connection
cnn.Open "Provider=Microsoft.ACE.OLEDB.12.0;" & _
"Persist Security Info=False;" & _
"User ID=Admin;" & _
"Data Source=" & CurrentProject.Path & _
"\YourDb.accdb;"

cnn.Close
Set cnn = Nothing
End Sub
Sub ReadSchema()
Dim conn As ADODB.Connection
Dim rst As ADODB.Recordset
Set conn = New ADODB.Connection
'Jet
'With conn
' .Provider = "Microsoft.Jet.OLEDB.4.0"
' .ConnectionString = "Data source=" & App.Path &
"\sample.mdb"
' .Open
'End With
'SQL Server
With conn
.Provider = "SQLOLEDB"
.ConnectionString = "data source=batman;user id=s
a;initial catalog=Northwind"
.Open
End With
Set rst = conn.OpenSchema(adSchemaTables)
Do Until rst.EOF
Debug.Print "Catalog: " & rst!Table_Catalog
Debug.Print "Schema: " & rst!Table_Schema
Debug.Print "Table Name: " & rst!Table_Name
Debug.Print "Type: " & rst!Table_Type
Debug.Print "Date Created: " & rst!Date_Created
Debug.Print "Date Modified" & rst!Date_Modified
Debug.Print
rst.MoveNext
Loop
rst.Close
conn.Close

Set rst = Nothing
Set conn = Nothing

End Sub
Sub ConnectToURL()
On Error GoTo Except
Dim Connection As New ADODB.Connection
Connection.Provider = "Microsoft.Jet.OLEDB.4.0"
Const URL = "http://localhost/access/nwind.mdb"
Connection.ConnectionString = "URL=" & URL
Connection.Mode = adModeReadWrite
Connection.Open
MsgBox "Connected via " & Connection.Provider & _
" OLE DB Provider!", vbInformation
Exit Sub
Except:
MsgBox Err.Description, vbCritical
End Sub
Sub addUser()
Dim myConnection As ADOX.Catalog
Dim newUser As ADOX.User
Dim userName As String
Dim newPassword As String

Set myConnection = New ADOX.Catalog
myConnection.ActiveConnection = _
"Provider = Microsoft.Jet.OLEDB.4.0;" & _
"Data Source=C:\r.mdb;" & _
"Jet OLEDB:System database=C:\r.mdw;" & _
"User id=john;Password=;"
Set newUser = New ADOX.User
newUser.Name = userName
myConnection.Users.Append newUser
myConnection.Users(newUser.Name).changePassword "", ne
wPassword
End Sub
Public Sub ADOWipeOutAttribute(RecID)
' Establish a connection transfers.mdb
MyConn = "C:\mydb.mdb"

With New ADODB.Connection
.Provider = "Microsoft.Jet.OLEDB.4.0"
.Open MyConn
.Execute "Delete From tblTransfer Where ID = " &
RecID
.Close
End With
End Sub

Add Record
Sub exaRecordsetAddNew()
Dim db As Database
Dim rs As Recordset

Set db = CurrentDb
Set rs = db.OpenRecordset("Books")
Debug.Print "Current title: " & rs!Title
With rs
.AddNew ' Add new record
!ISBN = "0-000" ' Set fields
!Title = "New Book"
!PubID = 1
!Price = 100
.Update ' Save changes.
.Bookmark = rs.LastModified ' Go to new record
Debug.Print "Current title: " & rs!Title
End With

rs.Close

End Sub
Sub Add_Record()
Dim conn As ADODB.Connection
Dim myRecordset As ADODB.Recordset
Dim strConn As String
strConn = "Provider=Microsoft.Jet.OLEDB.4.0;Data Sourc
e=" & CurrentProject.Path & "\mydb.mdb"

Set myRecordset = New ADODB.Recordset
With myRecordset
.Open "Select * from Employees", _
strConn, adOpenKeyset, adLockOptimistic
.AddNew
!LastName = "Marco"
!FirstName = "Paulo"
!City = "Boston"
.MoveFirst
.Close
End With

Set myRecordset = Nothing
Set conn = Nothing
End Sub
Update Record
Sub Update_Record()
Dim conn As ADODB.Connection
Dim myRecordset As ADODB.Recordset
Dim strConn As String

strConn = "Provider=Microsoft.Jet.OLEDB.4.0;Data Sourc
e=" & CurrentProject.Path & "\mydb.mdb"

Set myRecordset = New ADODB.Recordset

With myRecordset
.Open "Select * from Employees Where LastName = 'Ma
rco'", _
strConn, adOpenKeyset, adLockOptimistic
.Fields("FirstName").Value = "A"
.Fields("City").Value = "D"
.Fields("Country").Value = "USA"
.Update
.Close
End With
Set myRecordset = Nothing
Set conn = Nothing
End Sub
Delete Record
Sub exaRecordsetDelete()
Dim db As Database
Dim rs As Recordset
Dim DeleteCt As Integer

Set db = CurrentDb
Set rs = db.OpenRecordset("Books Copy")

DeleteCt = 0

rs.MoveFirst
Do While Not rs.EOF
If rs!Price > 20 Then
rs.Delete
DeleteCt = DeleteCt + 1
End If
rs.MoveNext
Loop

rs.Close

Debug.Print Format$(DeleteCt) & " records deleted."
End Sub
Public Sub ADOUpdate()
Dim rs As ADODB.Recordset
Dim strSQL As String

Set rs = New ADODB.Recordset
strSQL = "SELECT CompanyName, Address, City FROM tblCom
pany WHERE (CompanyName = 'Liams Diner')"

rs.Open strSQL, CurrentProject.Connection, adOpenKeyset
, adLockOptimistic

With rs
!CompanyName = "Diner"
.Update
End With
rs.Close
End Sub
Public Sub RemoveCompany()
Dim rst As ADODB.Recordset
Dim strSQL As String
Set rs = New ADODB.Recordset

rs.Open "SELECT * FROM tblCompany WHERE CompanyID=14",
CurrentProject.Connection, adOpenStatic, adLockOptimistic
With rs
If .RecordCount > 0 Then
.Delete
End If
End With
rs.Close
Set rs = Nothing
End Sub
Sub FindProject()
Dim strSQL As String
Dim rst As ADODB.Recordset
Set rst = New ADODB.Recordset

rst.ActiveConnection = CurrentProject.Connection
rst.CursorType = adOpenStatic
rst.Open "Select * from Employees"

'Attempt to find a specific project
strSQL = "[EmployeeID] = " & 1
rst.Find strSQL

'Determine if the specified project was found
If rst.EOF Then
msgBox lngValue & " Not Found"
Else
msgBox lngValue & " Found"
End If
rst.Close
Set rst = Nothing
End Sub
Sub Find_WithFilter()
Dim conn As ADODB.Connection
Dim rst As ADODB.Recordset

Set conn = New ADODB.Connection
conn.Open "Provider=Microsoft.Jet.OLEDB.4.0;Data Sourc
e=" & CurrentProject.Path & _
"\mydb.mdb"

Set rst = New ADODB.Recordset
rst.Open "Employees", conn, adOpenKeyset, adLockOptimi
stic
rst.Filter = "TitleOfCourtesy ='Ms.' and Country ='USA
'"
Do Until rst.EOF
Debug.Print rst.Fields("LastName").Value
rst.MoveNext
Loop

rst.Close
Set rst = Nothing
conn.Close
Set conn = Nothing
End Sub


----
-------

Anda mungkin juga menyukai