Solved

Access: How to run code before file close (apposite of AutoExec)?

Posted on 2016-08-17
5
46 Views
Last Modified: 2016-08-17
I'd like to do some clean-up of temporary tables whenever the user closes the Access file. Is there a way to do this, maybe like the AutoExec macro does for opening, but preferably in VBA?
TIA!
Dennis
0
Comment
Question by:Dennis_Gundersen
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
5 Comments
 
LVL 58

Accepted Solution

by:
Jim Dettman (Microsoft MVP/ EE MVE) earned 500 total points
ID: 41759515
There is nothing built-in, but you can achieve this easily by opening a hidden form at startup.   It will be the first form to close when a close is started.   You can use the OnClose or OnUnload events.

 From there, you can close any other objects, do cleanup, etc.

The other method is to disable the close on the main access window and provide your own means of starting an app close within the app.  Doing it this way, you don't need an additional form.

Jim.
0
 

Author Comment

by:Dennis_Gundersen
ID: 41759521
OK, thanks. It's a bit more involved than I would have guessed, but shouldn't be a problem.
Re
D
0
 
LVL 58
ID: 41759547
Note that you can cancel the close of Access by using Cancel = True in the OnUnload event if you find anything you don't like.  

Say the user has left forms or reports open other than a switchboard/menu form.

Use the OnClose event for clean-up once you've shut everything down you want in the OnUnload.

Jim.
0
 
LVL 58
ID: 41759562
By the way, that form is a nice place to store things as well, such as a user login name.  Or things like a company name and info.  

You can do that to avoid having to lookup data all the time.

It's also a great way to keep a persistent connection open to a backend, which if JET/ACE based, can give you a pretty big performance boost (depends on how your app works now).

Jim.
0
 
LVL 75
ID: 41759854
Side note ... this is a long standing request for an On Application Close event (et al).  It is now under consideration by the Access Product Team ... per access.uservoice.com
0

Featured Post

Instantly Create Instructional Tutorials

Contextual Guidance at the moment of need helps your employees adopt to new software or processes instantly. Boost knowledge retention and employee engagement step-by-step with one easy solution.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article describes two methods for creating a combo box that can be used to add new items to the row source -- one for simple lookup tables, and one for a more complex row source where the new item needs data for several fields.
AutoNumbers should increment automatically, without duplicates.  But sometimes something goes wrong, and the next AutoNumber value is a duplicate.  This article shows how to recover from this problem.
What’s inside an Access Desktop Database. Will look at the basic interface, Navigation Pane (Database Container), Tables, Queries, Forms, Report, Macro’s, and VBA code.
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

690 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question