Others

Prevent Users from Closing a Microsoft Access Database with the Close Button

Olivia MillerOlivia Miller Nov 14, 2026 868 views

Question details

Prevent users from closing a Microsoft Access database with the window Close button while retaining an approved shutdown procedure.

Prevent Users from Closing a Microsoft Access Database with the Close Button
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.
Before you start

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.

Solution 1Recommended

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.

1
Create the close flag

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

2
Create the guard form

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

3
Start the guard automatically

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.

4
Add an approved exit action

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.

5
Test both exit paths

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 a hidden form to control when Access can close
Limitations: This controls normal Access shutdown; it is not a security boundary and cannot prevent forced process termination or all startup bypasses. If an error handler must perform a controlled shutdown, set blnAllowClose to True before it calls DoCmd.Quit.
Free Microsoft Office alternative

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.

Work with Microsoft Word, Excel, and PowerPoint formats, including DOCX, XLSX, and PPTX.Use a familiar interface to ease migration from Microsoft Office.Keep document and spreadsheet work separate from Access database administration.Access essential office tools in a lightweight suite.
microsoft office alternative - wps office

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.