JEDEL
I pulled this code out of an application of mine that is currently in production. It includes a class called clsDataBase and a sub routine that will pull data from a client database through the class.
I hope the following code helps.
'This a class I call clsDataBase
Option Explicit
Public rs As New ADODB.Recordset
Public cnn1 As New ADODB.Connection
Public cmd As New Command
Public objVariable As New clsVariables
Public Sub Connect()
Public strError as String
'Check if a connection is already open
'This will occure if you have left it open for a previous query
If cnn1.state <> 0 Then 'if the object is open then close it
cnn1.Close
End If
'Open a connection using the Microsoft ODBC provider for jet.
Set cnn1 = New ADODB.Connection
Set cmd = New Command
cnn1.Provider = "Microsoft.Jet.OLEDB.4.0"
cnn1.ConnectionString = App.Path & "LivingClients.mdb"
cnn1.Open
End Sub
Public Sub selData(Query As String)
'This Sub Selects The Data
Dim cmd As New Command
Dim SQL As String
Set rs = New ADODB.Recordset
'First open a connection
Connect
Declare a new recordset
Set rs = New ADODB.Recordset
rs.CursorType = adOpenKeyset
rs.LockType = adLockOptimistic
Set cmd.ActiveConnection = cnn1
cmd.CommandText = Query
rs.CursorLocation = adUseClient
rs.Open cmd, , adOpenStatic, adLockBatchOptimistic
End Sub
Public Sub upDate(Query As String)
'This Sub Updates Data
Dim objDB As New clsDataBase
Connect
Set rs = New ADODB.Recordset
Set cmd = New ADODB.Command
Set cmd.ActiveConnection = cnn1
cmd.CommandText = Query
cnn1.Execute Query
End Sub
Public Sub insData(Query As String)
'This Sub Inserts Data
Dim objDB As New clsDataBase
Dim errLoop
Dim strError As String
Connect
Set rs = New ADODB.Recordset
Set cmd = New ADODB.Command
Set cmd.ActiveConnection = cnn1
cmd.CommandText = Query
On Error Resume Next
cnn1.Execute Query
'check for errors
' Enumerate Errors collection and display
' properties of each Error object.
For Each errLoop In cnn1.Errors
strError = "Error #" & errLoop.Number & vbCr & _
" " & errLoop.Description & vbCr & _
" (Source: " & errLoop.Source & "

" & vbCr & _
" (SQL State: " & errLoop.SQLState & "

" & vbCr & _
" (NativeError: " & errLoop.NativeError & "

" & vbCr
Next
Resume Next
cnn1.close
End Sub
Public Sub delData(Query As String)
'This Sub Deletes Data
Dim objDB As New clsDataBase
Dim objLaw As New clsLawformsMain
Connect
Set rs = New ADODB.Recordset
Set cmd = New ADODB.Command
Set cmd.ActiveConnection = cnn1
cmd.CommandText = Query
cnn1.Execute Query
'check for errors
' Enumerate Errors collection and display
' properties of each Error object.
For Each errLoop In cnn1.Errors
strError = "Error #" & errLoop.Number & vbCr & _
" " & errLoop.Description & vbCr & _
" (Source: " & errLoop.Source & "

" & vbCr & _
" (SQL State: " & errLoop.SQLState & "

" & vbCr & _
" (NativeError: " & errLoop.NativeError & "

" & vbCr
Next
Resume Next
cnn1.close
End Sub
'I use the following code to iterate through a retrieved recordset
'The table name in the database is tblClient and i am inserting the data into a
'listbox control
sub GetClientList()
dim objDB as New clsDataBase
dim SQL as string
SQL = "Select clientid, firstname, lastname from tblClient
objDB.selData SQL
Do While Not objDB.rs.EOF
list1.additem objDB.rs("clientid"

& ", " & objDB.rs("firstname"

& ", " objDB.rs("lastname"

objDB.rs.MoveNext
Loop
End Sub