
Do you have users who accidentally close the database when they try to close a form or report?
You know, those users who click the red X (the one that closes the application) by mistake. They don’t have to be inconvenienced by a single click in the wrong place.
Here’s how
The hidden form
The picture above shows a hidden form I put in all the databases I build.
How does it work?
The form is called fhdnNoClose. It is the second form opened when the database opens (the 1st form is the startup form). fhdnNoClose opens in hidden mode. When the user clicks the red X in the top right hand corner of the application, they see a message asking them if they want to exit the application.
Here’s how it works in my FruitMarket app
When users click the red X, they see a message asking for confirmation that they want to exit.
Asking the user if they want to exit the app.
If they click Cancel or press Esc, the app stays open. It they press Enter or click X or Exit, the app closes.
Here’s the code.
Private Sub cmdHide_Click()
Me.Visible = False
End Sub
Private Sub Form_Unload(Cancel As Integer)
'if developer checkbox ticked, just close this form
On Error GoTo Err_Handler
'Last mod date 24/04/2010
'tested
'allow developer to close this form
If Me.chkAllowDeveloperCloseThisForm = False Then
Cancel = True
If Me.chkAlreadyAskedToExit = True Then
Call CloseAndQuit
Else
DoCmd.OpenForm "frmAskBeforeExit", WindowMode:=acDialog
If Me.chkExitNow = True Then
Call CloseAndQuit
Else 'user clicked to cancel the exit
Cancel = True
End If
End If
End If
Exit_Handler:
Exit Sub
Err_Handler:
Select Case Err.Number
Case 2467 'The expression you entered refers to an object that is closed or doesn't exist.
Resume Next
Case Else
Debug.Print Err.Number, Err.Description, "Form_Unload", pstrMdl, Now
End Select
Resume Exit_Handler
End Sub
CloseAndQuit is a public sub that restores any database-wide settings to the user’s own preferences that were in place before they opened the FruitMarket app, then closes the app.
The code on frmAskBeforeExit is very simple.
Private Sub cmdCancel_Click()
On Error GoTo Err_Handler
'Last mod date 27/04/2010
'tested
DoCmd.Close acForm, Me.Name
Forms!fhdnNoClose.chkAlreadyAskedToExit = False
Exit_Handler:
Exit Sub
Err_Handler:
Debug.Print Err.Number, Err.Description, "cmdClose_Click", pstrMdl, Now
Resume Exit_Handler
End Sub
Private Sub cmdExit_Click()
On Error GoTo Err_Handler
'Last mod date 27/04/2010
'tested
Forms!fhdnNoClose.chkAlreadyAskedToExit = True
DoCmd.Close acForm, Me.Name
Exit_Handler:
Exit Sub
Err_Handler:
Debug.Print Err.Number, Err.Description, "cmdExit_Click", pstrMdl, Now
Resume Exit_Handler
End Sub