This article applies to a Microsoft Access database (.mdb) and to a Microsoft Access project (.adp).
For a Microsoft Access 2000 version of this article, see 245746 .
In order to disable the application Close button and the Close command on the System menu, you must call the GetSystemMenu and EnableMenuItem functions from the Win32 API.
In order to disable the Exit command on the File menu, you must use the CommandBars collection, which exposes all menu bars, toolbars, and shortcut menus in your application to Visual Basic for Applications so that you can manipulate them programmatically.
Microsoft provides programming examples for illustration only, without warranty either expressed or implied. This includes, but is not limited to, the implied warranties of merchantability or fitness for a particular purpose. This article assumes that you are familiar with the programming language that is being demonstrated and with the tools that are used to create and to debug procedures. Microsoft support engineers can help explain the functionality of a particular procedure, but they will not modify these examples to provide added functionality or construct procedures to meet your specific requirements.
- Start Microsoft Access.
- Open the sample database Northwind.mdb.
- On the Insert menu, click Module to create a new, standard module.
- Type the following code into the Declarations section:
Option Compare Database
Private Declare Function GetSystemMenu Lib "user32" (ByVal hWnd As Long, _
ByVal bRevert As Long) As Long
Private Declare Function EnableMenuItem Lib "user32" (ByVal hMenu As _
Long, ByVal wIDEnableItem As Long, ByVal wEnable As Long) As Long
Const MF_GRAYED = &H1&
Const MF_BYCOMMAND = &H0&
Const SC_CLOSE = &HF060&
Public Function SetEnabledState(blnState As Boolean)
'Disable the Menu Option
Sub ExitMenuState(blnExitState As Boolean)
Application.CommandBars("File").Controls("Exit").Enabled = blnExitState
'Disable the Close Button Option
Sub CloseButtonState(boolClose As Boolean)
Dim hWnd As Long
Dim wFlags As Long
Dim hMenu As Long
Dim result As Long
hWnd = Application.hWndAccessApp
hMenu = GetSystemMenu(hWnd, 0)
If Not boolClose Then
wFlags = MF_BYCOMMAND Or MF_GRAYED
wFlags = MF_BYCOMMAND And Not MF_GRAYED
result = EnableMenuItem(hMenu, SC_CLOSE, wFlags)
- On the File menu, click Save Northwind, and use the default name that appears in the Module Name box by clicking OK.
- Create a new form with the following characteristics:
Caption: Set Close State
OnClick: Event Procedure
OnClick: Event Procedure
- In Design view, right-click the Enable command button, and click Build Event on the menu that appears.
- Click Code Builder, click OK, and then type the following code in the resulting module:
Private Sub cmdEnable_Click()
- Add the following code for the Disable command button
Private Sub cmdDisable_Click()
- Save the Form and open it in Form View
UsageThe code described in this article allows you to easily enable or disable the Close button, the Close command of the application window, and the Exit command on the File menu to prevent users from exiting the application by using these methods.
Please note that this technique affects the Close button on the application window of Microsoft Access, not the Close button on the Database window. After disabling these options, neither is automatically re-enabled when your database closes. If the user closes the database and leaves Microsoft Access open, the user will not be able to quit Microsoft Access by using the Close button or the Exit command on the File menu. In this case, your application should re-enable both options before it terminates. Otherwise, the user will have to quit and restart Microsoft Access in order for the Close button and the Exit command on the File menu to be enabled.