Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 897817
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T14:56:36+00:00 2026-05-15T14:56:36+00:00

I am using Access 2003 to access data stored in a SQL Server database

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-15T14:56:37+00:00Added an answer on May 15, 2026 at 2:56 pm

    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
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 445k
  • Answers 445k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer From the JPA wiki: 1.4 Stored Procedures JPA does not… May 15, 2026 at 6:49 pm
  • Editorial Team
    Editorial Team added an answer Since you have a clear domain model (balls, pucks, etc)… May 15, 2026 at 6:49 pm
  • Editorial Team
    Editorial Team added an answer You still make some of the mistakes in the code… May 15, 2026 at 6:49 pm

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.