Solved

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

Posted on 2016-08-17
5
30 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
  • 3
5 Comments
 
LVL 57

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 57
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 57
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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

In the previous article, Using a Critera Form to Filter Records (http://www.experts-exchange.com/A_6069.html), the form was basically a data container storing user input, which queries and other database objects could read. The form had to remain op…
Introduction When developing Access applications, often we need to know whether an object exists.  This article presents a quick and reliable routine to determine if an object exists without that object being opened. If you wanted to inspect/ite…
In Microsoft Access, learn the trick to repeating sub-report headings at the top of each page. The problem with sub-reports and headings: Add a dummy group to the sub report using the expression =1: Set the “Repeat Section” property of the dummy…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

760 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now