I'm trying to return a ADO recordset from a function that can be iterated, have records counted, etc. I can return the recordset if I do not close either the recordset or connection but know this to be bad practice as well as causing the MDB file to bloat. I've seen some solutions on the Net but none of them applied to Access VBA. An over simplified version of my code is as follows.
Code:
Public Function QueryServer(ByVal sSql as String) As ADODB.Recordset
Dim cn as New ADODB.Connection
Dim rs as New ADODB.Recordset
cn.CursorLocation = adUseClient
rs.Cursor = adUseClient
cn.Open = "File Name=MyUDLFilePath"
rs.Open sSql, cn, adOpenStatic
Set QueryServer = rs
cn.Close
End Function
Private Sub Command0_Click
Dim sSql as String
Dim rs as ADODB.Recordset
sSql = "SELECT * FROM SomeTable"
Set rs = QueryServer(sSql)
Do While Not rs.EOF
Debug.Print rs.Fields(0).Value
Loop
rs.Close
End Sub