You said you want to keep the number of global variables as low as possible, but have you thouht about making the recordsets global.
If you do that you're able to reuse a recordset, I also made a separate function to connect so I don't need to reconnect over and over again:
To make the connection:
VB Code:
Sub connectDB(ByVal UserName As String, ByVal Password As String, ByVal DatabaseName As String)
'identify the new connection
Set con = New ADODB.connection
'if the connection is closed, open it
If con.State = adStateClosed Then
con.Provider = YourDBProvide
con.ConnectionString = YourConnectionString
con.Open
End If
End Sub
To open the recordset:
VB Code:
Sub OpenRec(ByVal rs As ADODB.Recordset, ByVal con As ADODB.connection, ByVal strSQL As String)
'open the recordset on remote
With rs
.ActiveConnection = con
.CursorLocation = adUseClient
.CursorType = adOpenStatic
.LockType = adLockBatchOptimistic
.Source = strSQL
.Open
End With
'disconnect recordset from connection
Set rs.ActiveConnection = Nothing
End Sub
keep in mind that my app runs on server/client, so I remove the connection to my recordset.
To update my recordset:
VB Code:
Sub UpdateRec(ByVal rs As ADODB.Recordset, ByVal con As ADODB.connection)
'reconnect recordset to connection
Set rs.ActiveConnection = con
'update recordset
rs.MarshalOptions = adMarshalModifiedOnly
rs.UpdateBatch
'disconnect recordset from connection
Set rs.ActiveConnection = Nothing
End Sub
Usage:
VB Code:
'To open a recordset
Set YourRecSet = New ADODB.Recordset
strSQL = "SELECT Blah Blah Blah ........."
Call OpenRec(YourRecSet, con, strSQL)
'Place your code here
'To update the recordset
UpdateRec YourRecSet,con