Results 1 to 5 of 5
  1. #1
    jmarchal1955 is offline Novice
    Windows 7 64bit Access 2013
    Join Date
    Jul 2014
    Posts
    2

    Handling Objects (Opening/Closing)

    I wonder if there are any best practices about handling objects in Access VBA. For example, if I have several modules that need to use an object, like FileScripting, or the same Recordsets for example. Is it preferred to create each object in each module and close it when done? Or is it preferred to create Public objects that can be used from anywhere? I have programmed a lot in Access VBA, and this has always been an issue for me. Thanks very much.

  2. #2
    June7's Avatar
    June7 is offline VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,930
    Objects take up memory. I prefer not to instantiate if they aren't needed. If the object is needed for every single procedure (or at least a lot of them) then instantiating when db opens might be efficient. Just depends on the object and how it is used. If you open a recordset but then edit the table(s) the recordset is based on, the edits will not be available in the recordset, unless maybe it is a RecordsetClone.

    Would there be any real gain in performance? I doubt it.

    Maybe copy/paste is a devil in disguise (so easy to replicate code) and one must make concerted effort not to get carried away with copy/paste and consolidate code whenever possible.
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  3. #3
    jmarchal1955 is offline Novice
    Windows 7 64bit Access 2013
    Join Date
    Jul 2014
    Posts
    2
    Thanks very much for your response June7. This has always been a concern, mostly because I want to write the best code I can. Good point about the Recordsets, I had not considered that. I was just concerned about the overhead to create, use, close, and set to "Nothing" each time I used an object. Thanks again.

  4. #4
    robrich22's Avatar
    robrich22 is offline Advanced Beginner
    Windows 7 64bit Access 2007
    Join Date
    Feb 2013
    Location
    Louisville, KY
    Posts
    41
    I would not personally advise to reuse any objects. When an object goes out of scope, the memory will be reclaimed. You do not need to worry about that. Declare the variables close as you can to where they are used and limit their scope to only what is necessary.

  5. #5
    ItsMe's Avatar
    ItsMe is offline Sometimes Helpful
    Windows 7 64bit Access 2010 32bit
    Join Date
    Aug 2013
    Posts
    7,862
    For automation it can be feasible to create a class module. I will typically copy and paste code snippets into forms' class modules as sub procedures before relying on a stand alone Class Module.

    An example where I have employed a class module would be using an API offered by a third party. When the application I am developing is centered around automation of another app, it makes sense to have the API available to many of the form's modules. Placing the API in a Standard Module is not efficient compared to using a Class Module where you can create and destroy objects, reserving system memory when needed and releasing it when finished.

Please reply to this thread with any new information or opinions.

Similar Threads

  1. Replies: 3
    Last Post: 04-19-2014, 02:35 AM
  2. Need help opening/closing Excel files
    By djrickel in forum Programming
    Replies: 1
    Last Post: 03-25-2014, 08:45 AM
  3. Issue w/ VBA code for Opening/Closing Forms
    By need_help12 in forum Forms
    Replies: 5
    Last Post: 04-20-2012, 11:09 AM
  4. Help with Null value handling
    By Thumbs in forum Programming
    Replies: 3
    Last Post: 03-05-2012, 10:55 AM
  5. Closing 1 form and opening another
    By mulefeathers in forum Forms
    Replies: 13
    Last Post: 12-08-2011, 04:04 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Other Forums: Microsoft Office Forums