How intercept close button on Access application

How does one intercept the user pressing the close button (Red X upper right) on an Access Application.  I would just like to ask the user to confirm that closing the application and exiting Access is what he really wants to do.
WoodrowAAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Eric ShermanAccountant/DeveloperCommented:
Here's one approach.  

I have a application where the users must use a logout button on the main form (frmMain) to logout and exit the application.  Therefore anytime they try to shut down  Access (Red X button or from the Task Bar at the botton of your screen) without using the logout button the Unload Event of frmMain will kick this error message and cancel the event.  I have a hidden control TxtForceLogoutButton on frmMain that is set to True (-1) when it opens and set to False (0) when the Logout Button is clicked.

Private Sub Form_Unload(Cancel As Integer)
If Me.TxtForceLogoutButton = -1 Then
    MsgBox "Please use the Logout button to close the application!!!!", vbInformation, ""
    DoCmd.CancelEvent
End If
End Sub


Simple but it works great.

ET
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Eric ShermanAccountant/DeveloperCommented:
Also,

In your case you really don't need a hidden control.  Just use the Unload Event of your main form with a Yes/No message box then perform the desired action based on  what the user selects.

Private Sub Form_Unload(Cancel As Integer)
    If MsgBox("Are you sure you want to close the application and exit Access??" & vbCrLf & vbCrLf & "Click Yes to Exit or No to Cancel", vbYesNo, "Exit Application!!!") = vbYes Then
        DoCmd.Quit
    Else
        DoCmd.CancelEvent
    End If
End Sub


ET
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft Access

From novice to tech pro — start learning today.