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

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
Dennis_GundersenAsked:
Who is Participating?

[Webinar] Streamline your web hosting managementRegister Today

x
 
Jim Dettman (Microsoft MVP/ EE MVE)Connect With a Mentor PresidentCommented:
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
 
Dennis_GundersenAuthor Commented:
OK, thanks. It's a bit more involved than I would have guessed, but shouldn't be a problem.
Re
D
0
 
Jim Dettman (Microsoft MVP/ EE MVE)PresidentCommented:
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
 
Jim Dettman (Microsoft MVP/ EE MVE)PresidentCommented:
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
 
DatabaseMX (Joe Anderson - Microsoft MVP, Access and Data Platform)Commented:
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
All Courses

From novice to tech pro — start learning today.