Microsoft KB Archive/100463

From BetaArchive Wiki

INF: How to Disable the Close Option on the System Menu

PSS ID Number: Q100463 Article last modified on 09-20-1993

1.00 1.10 WINDOWS

The information in this article applies to:
- Microsoft Access versions 1.0 and 1.1


To prevent a user from manually closing a form, you have two options: 1. Use the CancelEvent macro action with the OnClose event on a form. 2. Disable the Close option on the System menu, using a function on the OnOpen event of the form. The second approach, which is described in this article, requires the use of Windows application programming interface (API) calls, but is more effective. It prevents the user from choosing the Close option and does not require special handling of the OnClose event when the Close action is invoked. This article assumes that you are familiar with Access Basic and with creating Microsoft Access applications using the programming tools provided with Microsoft Access. For more information on Access Basic, please refer to the “Introduction to Programming” manual. NOTE: In this article, error trapping has been minimized.


To disable the Close option on the System menu, create a module in your database containing the DisableCloseMenu() function, then call the function from the OnOpen event in your form. 1. Start Microsoft Access and open the sample database NWIND.MDB. 2. Create a new form called Form1. 3. Place a command button on the form. 4. Set the properties for the form and button as follows: Object: Command Button ====================== ControlName: Button1 Caption: Close OnPush: TestClose Object: Form ============ On Open: =DisableCloseMenu(Form) 5. Save and close the form. 6. Create the following macro and call it TestClose. This macro, along with the command button on the form, enables the user to close the form. Macro Name Action ===================== TestClose Close TestClose Actions ——————— Close Object Type: Form Object Name: Form1 7. Create a new module called Module1. Enter the following code: ‘*****************************************************************’ Declarations section of the module ‘*****************************************************************’ NOTE: In the following sample code, an underscore () is used as a ’ line-continuation character. Remove the underscore when re-creating ’ this code in Access Basic. ’ NOTE: You may have some Windows API functions defined in an existing ’ Microsoft Access library; therefore, your declarations may be duplicates. ’ If you receive the duplicate procedure name error message, remove or ’ comment the declarations statement from your code. Option Explicit Const SC_CLOSE = &HF060 Const MF_GRAYED = 1 Declare Function GetSystemMenu% Lib “user” (ByVal hWnd%, ByVal flag%) Declare Function EnableMenuItem% Lib “user” (ByVal hMenu%,ByVal nItem%, ByVal nAction%) ‘=========================================================== ’The following function retrieves the handle to the System ’menu and dims the Close option.’=========================================================== Function DisableCloseMenu (frm As Form) Dim hSysMenu As Integer Dim D As Integer hSysMenu = GetSystemMenu(frm.hWnd, 0) d = EnableMenuItem(hSysMenu, SC_CLOSE, MF_GRAYED) End Function NOTE: When you are determining the position of a menu command, separator bars count as commands. For example, to get to the Import menu command on the File menu in the Database window, use the arguments 0 (for the File menu) and 7 (for the Import command). Positions 3 and 6 correspond to the separator bars on the File menu. 8. Give Form1 the focus. Note that double-clicking the System menu does not close the form and that the Close option on the form’s System menu is unavailable (dimmed). Choose the Close button to close the form.


For more information about disabling menu items, query on the following words here in the Microsoft Knowledge Base: menu and gray and getmenu

Additional reference words: 1.00 1.10 UI forms SDK menu close gray grey KBCategory: KBSubcategory: PrgrmOthr GnrlNw Copyright Microsoft Corporation 1993.