Solved

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

Posted on 2016-08-17
5
42 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 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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Preparing an email is something we should all take special care with – especially when the email is for somebody you may not know very well. The pressures of everyday working life stacked with a hectic office environment can make this a real challen…
Access custom database properties are useful for storing miscellaneous bits of information in a format that persists through database closing and reopening.  This article shows how to create and use them.
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.
With Microsoft Access, learn how to start a database in different ways and produce different start-up actions allowing you to use a single database to perform multiple tasks. Specify a start-up form through options: Specify an Autoexec macro: Us…

730 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