Prevent Users from Closing a Microsoft Access Database with the Close Button
Question details
Prevent users from closing a Microsoft Access database with the window Close button while retaining an approved shutdown procedure.

- Product
- Microsoft Access
- Device & OS
- not provided
- Scenario
- An Access database uses its own close procedure to control how the application shuts down.
- Observed behavior
- Users should be blocked from closing Access with the window Close button and should exit through the approved procedure instead.
Make sure you can edit the database's VBA modules, forms, and startup macro. Enable trusted content or use a trusted location so the startup macro and VBA code can run.
Use a hidden form to control when Access can close
Use this approach when you want to cancel ordinary attempts to close Access until your database's approved exit procedure permits them.
A hidden, unbound form remains open during the session. Its Unload event cancels closure unless a public Boolean flag has been set to True.
In Access, open Create > Module and add the following code to a standard module: Public blnAllowClose As Boolean Public Function InitCloseGuard() As Boolean blnAllowClose = False InitCloseGuard = True End Function
Select Create > Form Design and save the unbound form as frmCloseGuard. Open its property sheet, select the Event tab, and set On Unload to [Event Procedure]. Select the build button and add this code: Private Sub Form_Unload(Cancel As Integer) Cancel = Not blnAllowClose End Sub
Create or edit the AutoExec macro using Create > Macro. Add a RunCode action with the function name InitCloseGuard(), followed by an OpenForm action for frmCloseGuard with Window Mode set to Hidden. Save the macro as AutoExec.
In the VBA event procedure for your database's Exit or Close button, set blnAllowClose = True immediately before calling DoCmd.Quit acQuitSaveAll. This option saves objects without prompting. If the shutdown fails and the database remains open, set the flag back to False in the error handler.
Reopen the database so AutoExec runs, then select the Access window's Close button and confirm the guard prevents closure. Use your database's approved Exit button to confirm that Access closes normally.

Use WPS Office for Documents, Spreadsheets, and Presentations
WPS Office is a free, lightweight alternative with a familiar interface for everyday Office files and an easy transition from Microsoft Office. It does not replace Microsoft Access or implement its database close controls.

Frequently Asked Questions
Does this method disable the Access Close button?
No. The button remains visible, but the hidden form's On Unload event cancels normal closure while blnAllowClose is False.
Why doesn't the hidden form open when the database starts?
Check that the startup macro is named AutoExec and contains an OpenForm action for frmCloseGuard with Window Mode set to Hidden. Also check that the database is trusted and its content is enabled.
How can an approved exit button close Access?
In the button's VBA event procedure, set blnAllowClose = True before calling DoCmd.Quit. If the quit operation fails and Access remains open, reset the flag to False in the error handler.
Can users still force Access to close?
Yes. The form's Unload event controls normal shutdown, not forced process termination. It should not be treated as a security feature.




