Search code examples
ms-accessadorecordset

Proper way to close ADO recordset in Access when passing to calling method?


I am using Access 2003 to access data stored in a SQL Server database via ADO. Most often I obtain the data using stored procedures. I execute the stored procedures via ADO in a separate function that returns a recordset. When returning a recordset from a function where is the best place to close the recordset and release it's hold on memory? Is it done in the function that returns the recordset or is it done in the sub/function that calls the function executing the ADO code? Here is an example ...

Here is the calling method

Public Sub CallingMethod()
  Dim rs As ADODB.Recordset
  Set rs = GetDataFromDatabase()
  Set myListBox.Recordset = rs
  rs.Close
  Set rs = Nothing
End Sub

Here is the Method that actually executes the stored procedure and returns it to the calling method

Public Function GetDataFromDatabase()
  Dim cnn As ADODB.Connection
  Dim rs As ADODB.Recordset

  Set cnn = New ADODB.Connection
  cnn.ConnectionString = myConnectionString
  cnn.Open

  Set rs = New ADODB.Recordset
  Set rs.ActiveConnection = cnn
  rs.Source = "EXEC uspMyStoredProcedure"

  rs.LockType = adLockOptimistic
  rs.CursorType = adOpenStatic
  rs.CursorLocation = adUseClient
  rs.Open

  Set GetDataFromDatabase = rs

  Set rs = Nothing
  Set cnn = Nothing

End Function

What I am wondering is where I should be closing the recordset and setting it equal to nothing. Am I doing it in both places without needing to? Just looking for best practices. Thank you.


Solution

  • I'm not sure what's proper, either. I tried an approach where the calling code creates the connection object and passes it to the function. The caller is responsible for disposing of both the connection and recordset objects. It's quick & dirty because I didn't want to invest more in it only to find out my approach is just plain boneheaded.

    Public Sub test_GetDataFromSP()
        Dim cnn As ADODB.Connection
        Dim rs As ADODB.Recordset
    
        Set cnn = New ADODB.Connection
        cnn.ConnectionString = "DRIVER=SQL Server;SERVER=VM2003\SQLEXPRESS;" & _
            "Trusted_Connection=Yes;DATABASE=Inventory"
        cnn.Open
        Set rs = GetDataFromSP("GetCenterCodes", cnn, "14, 14, 501")
        rs.MoveLast
        Debug.Print rs.RecordCount
        rs.Close
        Set rs = Nothing
        cnn.Close
        Set cnn = Nothing
    End Sub
    
    Public Function GetDataFromSP(ByVal pProc As String, _
        ByRef pConnection As ADODB.Connection, _
        Optional ByVal pArguments As String) As ADODB.Recordset
    
        Dim rs As ADODB.Recordset
        Dim strStatement As String
    
        strStatement = "EXEC " & pProc
        If Len(pArguments) > 0 Then
            strStatement = strStatement & " " & pArguments
        End If
        strStatement = strStatement & ";"
        Set rs = New ADODB.Recordset
        Set rs.ActiveConnection = pConnection
        rs.Source = strStatement
        rs.LockType = adLockOptimistic
        rs.CursorType = adOpenStatic
        rs.CursorLocation = adUseClient
        rs.Open
        Set GetDataFromSP = rs
    End Function