Closing a Recordset vs. Setting to Nothing

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • twinnyfo
    Recognized Expert Moderator Specialist
    • Nov 2011
    • 3662

    Closing a Recordset vs. Setting to Nothing

    Hey Friends,

    This is more of a conceptual question rather than a problem. During my experience in Access VBA coding, I have seen various methods for closing one's session with a Recordset. I have seen some coders "close" the Recordset:

    Code:
    rstMyRecords.Close
    and I've seen others set the Recordset equal to "Nothing" like this:

    Code:
    Set rstMyRecords = Nothing
    Is there any type of substantive difference between these two methods and/or are there any advantages of one over the other? I know the bottom line is that good coding always does "something" to tell the DB that it is done with that Recordset, but I've just never heard discussion about these two ways of doing that.

    Any insight would be appreciated, as I am always willing to learn more about how all this stuff works.

    Thanks and warmest regards,
    twinnyfo
  • Seth Schrock
    Recognized Expert Specialist
    • Dec 2010
    • 2965

    #2
    You might check out this thread: Closing a recordset in VBA. The last part of the thread goes off on the CurrentDb trail, but I think that the first couple of posts might help somewhat.

    Comment

    • ADezii
      Recognized Expert Expert
      • Apr 2006
      • 8834

      #3
      To the best of my knowledge, Closing a Recordset does not release the Resources assigned to its Object Variable, thus you must set this Variable to Nothing. Many times, especially in Error Handling Code, you may not know if a Recordset actually exists, so:
      Code:
      If Not rsPCs Is Nothing Then
        rsPCs.Close
          Set rsPCs = Nothing
      End If

      Comment

      • zmbd
        Recognized Expert Moderator Expert
        • Mar 2012
        • 5501

        #4
        ADezii is correct.
        My CompSci Prof taught:
        "Rule of thumb... if you open it, close it.
        If you assign it... release it."

        If you fail to follow: In the best of cases nothing happens, in the worst you get memory leaks, unavailable resources, and corrupted data files.
        I've found Access to be very forgiving, often closing and releasing resources implicitly; however, it’s not best practice and really puts a lot of faith in other programmers to cleanup.

        Comment

        • twinnyfo
          Recognized Expert Moderator Specialist
          • Nov 2011
          • 3662

          #5
          Hey, guys!

          Thanks for the info. This is exactly what I was looking for!

          ADezii, this helped me understand the concept easily.

          Seth, your links gave me additional insight into these concepts, and now I need to clean up my DB....

          Z, good principles to live by!

          Thanks again to all!
          -Twinnyfo

          Comment

          Working...