How to trap User quitting by clicking on the X button or Exit Access menu button?

R

Rick

I have an app which does some logging when the User ends the session. I use
a switchboard and have an "Exit and Close Application" button. But if they
quit by clicking the red X (Windows application quit) or by clicking on the
Exit Access the code doesn't get executed.

Is there a way to trap the event so that I can pass it to the logging
function?

Thanks.


.... rick
 
D

Douglas J. Steele

What you can do is create a module-level variable (let's call it
mbooCanQuit). You'd do this by putting the following line of code at the
very top of the form's class module (after the Option statements):

Option Compare Database
Option Explicit

Private mbooCanQuit As Boolean

That variable will automatically be set to False when the form opens, or you
can set it explicitly if you like:

Private Sub Form_Load()

mbooCanQuit = False

End Sub


When the user clicks on the "Exit and Close Application" button, set the
variable to True.

Private Sub cmdExit_Click()

' your existing code

mbooCanQuit = True

End Sub

In the form's Unload event, set Cancel = True if mbooCanQuit is not True:

Private Sub Form_Unload(Cancel As Integer)

If mbooCanQuit = False Then
MsgBox "You must using the 'Exit and Close Application' button " & _
"to shut down."
Cancel = Not mbooCanQuit
End If

End Sub
 
R

Rick

Doug, this method traps the clicks but when the 'exit and close database'
button is clicked the application closes yet the message box appears even
after Access has terminated. Any suggestions?

Thanks
.... rick
 
R

Rick

OK, fixed it by putting a docmd.close acform, me.name just before the
application.quit statement.
 
K

Ken Sheridan

Rick:

How about calling the logging function from the switchboard form's Close
event procedure. That way it will be called however the user closes the
application.

Another approach, where you can't assume that the switchboard, or
equivalent, will not have been closed before the user closes the application,
is to open a hidden unbound form at start-up and call the logging function
from its Close event procedure.

Ken Sheridan
Stafford, England
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top