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.