Hello
Yes, you can reuse a recordset in different modules. Declare the recordset as a public variable in a standard module and initialize it in a subroutine. Then, access it from other modules. Here’s a quick example:
Declare Public Variables:
' In Module1
Public rs As ADODB.Recordset
Public conn As ADODB.Connection
Initialize Recordset:
Sub InitializeRecordset()
Set conn = New ADODB.Connection
conn.Open "Your_Connection_String"
Set rs = New ADODB.Recordset
rs.Open "SELECT * FROM YourTable", conn, adOpenStatic, adLockReadOnly
End Sub
Access in Other Modules:
' In Module2
Sub UseRecordsetModule2()
If Not rs Is Nothing Then
Do While Not rs.EOF
Debug.Print rs.Fields("YourFieldName").Value
rs.MoveNext
Loop
End If
End Sub
' In Module3
Sub UseRecordsetModule3()
If Not rs Is Nothing Then
rs.MoveLast
rs.MoveFirst
Debug.Print "Record count: " & rs.RecordCount
End If
End Sub
To open the recordset in Module1 before using it in Module2 or Module3. You can check out VBA Express.
Hope it helps !
thank you
gregbowers