Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations biv343 on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Have code/macro run when app is closed regrdls of method

Status
Not open for further replies.
Aug 2, 2000
325
US
I need either a macro or vba to run when the application is closing. Regardless of how access is closing. like if the Close button is used OR if they use a command button on a form. Where would that code go?

Does this make sense?

Thanks,
Dave
 
One way to handle this just occurred to me:

Create a form, and hide it from the user.

Cause the form to be opened when Access starts (e.g. by the AutoExec macro).

Access will automatically close the form when the user exits.

Have the hidden form's OnClose event execute your macro.
 
Thanks beetee,
That's exactly what I ended up doing. I was hopeing that there is another way, one that doesn't force a form to be open and running in the back ground - resources and all.

Thanks again,
Dave
 
Dave,

This is quite a common strategy. One nice benefit of it is that you can use it to control how users exit your application. If you do this in the form that's going to stay open:
Private Sub Form_Unload(Cancel As Integer)
On Error GoTo Error
If Me.tag = "AllowExit" Then
Cancel = True
End If
Exit Sub
Error:
ErrorTrap Err.Number, Err.Description, "Form Unload", "frmSwitchboard"
End Sub

then you can make it so that the only way that the tag gets set to "AllowExit" is through the closing of all forms and reports. You'll of course want to give yourself a back door to use while you're testing, because there will be times when things hang. But it makes it possible to run any cleanup code you want to run every time the user closes the database (except with a three-finger salute or a kick to the power cord).

Jeremy =============
Jeremy Wallace AlphaBet City Dataworks See the Developers' section for some helpful fundamentals.

See thread181-473997 for information on how to get the best responses possible here at Tek-Tips.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top