You are currently offline, waiting for your internet to reconnect

How to disable the Close button on the Application window and the Exit command on the File menu

Support for Office 2003 has ended

Microsoft ended support for Office 2003 on April 8, 2014. This change has affected your software updates and security options. Learn what this means for you and how to stay protected.

This article was previously published under Q300688
Advanced: Requires expert coding, interoperability, and multiuser skills.

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.
For a Microsoft Access 97 version of this article, see 258049.
Microsoft Access has no built-in method for disabling the Close button (X) on the application window, the Close command on the System menu of the application window, or the Exit command on the File menu. This article describes how to programmatically disable all of these.
CAUTION: If you follow the steps in this example, you modify the sample database Northwind.mdb. You may want to back up the Northwind.mdb file and follow these steps on a copy of the database.

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.

Step-by-Step Example

  1. Start Microsoft Access.
  2. Open the sample database Northwind.mdb.
  3. On the Insert menu, click Module to create a new, standard module.
  4. Type the following code into the Declarations section:
    Option Compare DatabaseOption ExplicitPrivate Declare Function GetSystemMenu Lib "user32" (ByVal hWnd As Long, _    ByVal bRevert As Long) As LongPrivate Declare Function EnableMenuItem Lib "user32" (ByVal hMenu As _    Long, ByVal wIDEnableItem As Long, ByVal wEnable As Long) As LongConst MF_GRAYED = &H1&Const MF_BYCOMMAND = &H0&Const SC_CLOSE = &HF060&Public Function SetEnabledState(blnState As Boolean)    Call CloseButtonState(blnState)    Call ExitMenuState(blnState)End Function'Disable the Menu OptionSub ExitMenuState(blnExitState As Boolean)    Application.CommandBars("File").Controls("Exit").Enabled = blnExitStateEnd Sub'Disable the Close Button OptionSub 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    Else        wFlags = MF_BYCOMMAND And Not MF_GRAYED    End If        result = EnableMenuItem(hMenu, SC_CLOSE, wFlags)End Sub					
  5. On the File menu, click Save Northwind, and use the default name that appears in the Module Name box by clicking OK.
  6. Create a new form with the following characteristics:
       Form: frmSetCloseState   -------------------------   Caption: Set Close State   Command button   ------------------------   Name: cmdEnable   Caption: Enable   OnClick: Event Procedure   Command button   ------------------------   Name: cmdDisable   Caption: Disable   OnClick: Event Procedure					
  7. In Design view, right-click the Enable command button, and click Build Event on the menu that appears.
  8. Click Code Builder, click OK, and then type the following code in the resulting module:
    Private Sub cmdEnable_Click()    Call SetEnabledState(True)End Sub					
  9. Add the following code for the Disable command button
    Private Sub cmdDisable_Click()    Call SetEnabledState(False)End Sub					
  10. Save the Form and open it in Form View
Note that the Disable command button, the Close button, the Close command of the application window, and the Exit command on the File menu are disabled. If you click the Enable command button, these commands will be re-enabled.


The 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.
OfficeKBHowTo inf gray grayed grey greyed out upper right unavailable ACC2002 ACC2003

Article ID: 300688 - Last Review: 01/31/2007 19:15:28 - Revision: 6.2

  • Microsoft Office Access 2003
  • Microsoft Access 2002 Standard Edition
  • kbvba kbprogramming kbhowto KB300688