Microsoft KB Archive/300688

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

Article ID: 300688

Article Last Modified on 1/31/2007

-

APPLIES TO


 * Microsoft Office Access 2003
 * Microsoft Access 2002 Standard Edition

-



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.



SUMMARY
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.



MORE INFORMATION
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
 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 Option Explicit

Private Declare Function GetSystemMenu Lib &quot;user32&quot; (ByVal hWnd As Long, _   ByVal bRevert As Long) As Long

Private Declare Function EnableMenuItem Lib &quot;user32&quot; (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) Call CloseButtonState(blnState) Call ExitMenuState(blnState) End Function

'Disable the Menu Option Sub ExitMenuState(blnExitState As Boolean) Application.CommandBars(&quot;File&quot;).Controls(&quot;Exit&quot;).Enabled = blnExitState End Sub

'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 Else wFlags = MF_BYCOMMAND And Not MF_GRAYED End If   result = EnableMenuItem(hMenu, SC_CLOSE, wFlags) End Sub  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: <pre class="fixed_text">  Form: frmSetCloseState -  Caption: Set Close State

Command button Name: cmdEnable Caption: Enable OnClick: Event Procedure

Command button Name: cmdDisable Caption: Disable OnClick: Event Procedure </li> In Design view, right-click the Enable command button, and click Build Event on the menu that appears.</li>  Click Code Builder, click OK, and then type the following code in the resulting module: Private Sub cmdEnable_Click Call SetEnabledState(True) End Sub </li>  Add the following code for the Disable command button Private Sub cmdDisable_Click Call SetEnabledState(False) End Sub </li> Save the Form and open it in Form View</li></ol>

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.

Usage
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.

Additional query words: OfficeKBHowTo inf gray grayed grey greyed out upper right unavailable ACC2002 ACC2003

Keywords: kbvba kbprogramming kbhowto KB300688

-

[mailto:TECHNET@MICROSOFT.COM Send feedback to Microsoft]

© Microsoft Corporation. All rights reserved.