VBA: benefit of closing objects and setting to nothing?

Anonymous
2024-06-08T21:53:12+00:00

I've heard and read debates on whether or not it is necessary to close objects and set their object variables to nothing. Some online commenters have said that while it was essential in the past to do so (e.g., pre-2007, leaving a recordset open when Access closed would require Task Manager to terminate Access), memory is now handled much better --- the result of which is that if an object is no longer needed in a procedure, the End Sub/End Function statement now does what "[object].Close" and "Set [object] = Nothing" previously did.

Besides allowing an object variable to be redefined or to prevent unintended changes to the object, is it essential or beneficial to close all objects and set their object variables to nothing before ending the sub or function when there is no further use for those objects? If no longer essential or beneficial, with which version of Office did that become the case? If it is essential or beneficial, why? Does doing so help reduce file-size bloat over time (due to temporary objects)? Thanks.

Microsoft 365 and Office | Access | For business | Windows

Locked Question. This question was migrated from the Microsoft Support Community. You can vote on whether it's helpful, but you can't add comments or replies or follow the question.

0 comments No comments
{count} votes

13 answers

Sort by: Most helpful
  1. Anonymous
    2024-06-09T23:51:05+00:00

    https://learn.microsoft.com/en-us/office/client-developer/access/desktop-database-reference/recordset-lockedits-property-dao

    I am afraid it is not such simple as you displayed and tested.

    Please understand the meaning of lock.

    You can refer one of some MS documents in above url.

    0 comments No comments
  2. Anonymous
    2024-06-09T23:58:32+00:00

    also with a huge recordset.

    You open it as table or dynaset move last 1000 times,it will stackoverflow.

    But if you open and paired with close,no problem happen.

    For the rust of your program,you'd better use close and nothing for cleaning.

    Of course,as to very simple codes as you have shown,it is not very necessory .

    But when in huge codes block and multiple process to access database,this is very important.

    0 comments No comments
  3. Anonymous
    2024-06-10T21:48:16+00:00

    Thanks for responding, GroverParkGeorge. I am of the same opinion and have the same practice. Every "open" should have a "close", nothing should be assumed or left to chance. I am looking for a technical justification (if there still is one) because I cannot convince younger developers with my "best practices" argument.

    Hi Will,

    I am not that strict: it depends on the scope of a variable.

    If an object is declared within a procedure, memory should be cleared after ending the procedure, and not by the developer.

    Global declared objects are something different, and hard to understand.

    Especially in my case, where I use quite a few global objects in a shared library database to build dynamical applications.

    Imb.

    0 comments No comments
  4. George Hepworth 22,295 Reputation points Volunteer Moderator
    2024-06-10T21:53:55+00:00

    I think enforcing consistency is a higher priority than relying on what you expect to have happen.

    0 comments No comments
  5. Anonymous
    2024-06-11T09:20:56+00:00

    I think enforcing consistency is a higher priority than relying on what you expect to have happen.

    Hi George,

    I am afraid that I interpret your answer in a different way than you meant.

    Can you elaborate a little more on that?

    Thank you.

    Imb.

    0 comments No comments