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?
 
Eric ShermanConnect With a Mentor Accountant/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
 
Eric ShermanConnect With a Mentor Accountant/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
All Courses

From novice to tech pro — start learning today.