Option Compare Database
Private Sub Form_Load()
Dim remoteConnection As New ADODB.Connection
Dim rsProducts As New ADODB.Recordset
Connect
SetRecordset
End Sub
Private Sub Form_Unload(Cancel As Integer)
Disconnect
End Sub
Public Sub Disconnect()
On Error GoTo ConnectionError
rsProducts.Close
remoteConnection.Close
Exit Sub
ConnectionError:
MsgBox "There was an error closing the databse." & _
Err.Number & ", " & Err.Description
End Sub
Private Sub Connect()
On Error GoTo ConnectionError
With remoteConnection
.Provider = "Microsoft.ACE.OLEDB.12.0"
.Open "C:\Users\S B\Documents\Databases\Northwind.accdb"
End With
Exit Sub
ConnectionError:
MsgBox "There was an error connecting to the database. " & _
Chr(13) & Err.Number & ", " & Err.Description
End Sub
Public Sub SetRecordset()
Dim sql As String
On Error GoTo DbError
sql = "select * from Products"
rsProducts.CursorType = adOpenKeyset
rsProducts.LockType = adLockReadOnly
rsProducts.Open sql, remoteConnection, _
, , adCmdText
If rsProducts.EOF = False Then
'Using three different techniques to access items in a recordset
Me.txtProductID = rsProducts!ID
Me.txtProductCode = rsProducts.Fields.Item("Product Code")
Me.txtProductName = rsProducts.Fields.Item(3)
End If
Exit Sub
DbError:
MsgBox "There was an error retrieving information " & _
"From the database." _
& Err.Number & ", " & Err.Description
End Sub
|