Disenabling Titlebar/Application Close Button

Disable Form Closing

home
Monday, December 30, 2002 21:47:40.
Modified Wednesday, January 01, 2003 16:03:49

This method was derived from two places. The portion disabling the close button in the databases' caption bar is derived from a Google newsgroup search dated May 14th 1999 (comp.databases.ms-access, entitled Disable "CloseApp" button). The portion disabling it form-by form was derived from Chapter 8, p483 of the Access 2000 Developer's Handbook. I found I had to make some minor tweaks to the Developer's Handbook instructions, but otherwise they worked perfectly.

Go to Disable Form Closing

Disable Databases' Close Button (in Title Caption Bar)

  1. Create Module

    You need to create a module, name it something like mdlDisenableCloseButton. The code follows:

    
    Option Compare Database
    Option Explicit
    'ACCESS WINDOW MENUS
    Public Declare Function GetWindowLong Lib "user32" Alias "GetWindowLongA" _
    (ByVal hWnd As Long, ByVal nIndex As Long) As Long
    Public Declare Function SetWindowLong Lib "user32" Alias "SetWindowLongA" _
    (ByVal hWnd As Long, ByVal nIndex As Long, ByVal dwNewLong As Long) As Long
    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
    Private Const MF_BYCOMMAND = &H0&
    Private Const MF_ENABLED = &H0&
    Private Const MF_GRAYED = &H1&
    Private Const SC_CLOSE = &HF060&
    Public Const WS_SYSMENU = &H80000
    Public Const GWL_STYLE = (-16)
    Public Const WS_CAPTION = &HC00000
    
    Function EnableCloseButton()
        Dim lngSystemMenuHandle As Long
        lngSystemMenuHandle = GetSystemMenu(Access.hWndAccessApp, False)
        Call EnableMenuItem(lngSystemMenuHandle, SC_CLOSE, MF_BYCOMMAND Or MF_ENABLED)
    End Function
    
    Function DisableCloseButton()
        Dim lngSystemMenuHandle As Long
        lngSystemMenuHandle = GetSystemMenu(Access.hWndAccessApp, False)
        Call EnableMenuItem(lngSystemMenuHandle, SC_CLOSE, MF_BYCOMMAND Or MF_GRAYED)
    End Function
    
    Function DisAbleTitleBarControls()
        Dim lngAccessWindowHandle As Long, lngWindowStatus As Long
        lngAccessWindowHandle = Access.hWndAccessApp
        lngWindowStatus = GetWindowLong(lngAccessWindowHandle, GWL_STYLE)
        lngWindowStatus = lngWindowStatus And (Not WS_CAPTION)
        Call SetWindowLong(lngAccessWindowHandle, GWL_STYLE, lngWindowStatus)
    End Function
    
    Function EnableTitleBarControls()
        Dim lngAccessWindowHandle As Long, lngWindowStatus As Long
        lngAccessWindowHandle = Access.hWndAccessApp
        lngWindowStatus = GetWindowLong(lngAccessWindowHandle, GWL_STYLE)
        lngWindowStatus = lngWindowStatus Or WS_CAPTION
        Call SetWindowLong(lngAccessWindowHandle, GWL_STYLE, lngWindowStatus)
    End Function

    Note: line-wrapping should be okay as it is if you copy & paste, but make sure

  2. Save the Module

    Save the module with a meaningful name like mdlDisenableCloseButton.

  3. Compile the Module

    With the module still open, select "Debug...Compile"

  4. Call the Function for Disenabling the Close Button

    With the module still open, open the "immediate window" by typing CTRL-G. In the ensuing window--which should appear somewhere near the bottom of the module--type:

    Call DisableCloseButton()

    and press ENTER

  5. Close the module, and you should see the application button dimmed out, thus no longer working.

  6. Execute this command everytime the database starts

    The feature automatically shuts itself off everytime you shut down the database. To make it execute upon startup, simply set it up so that the Call DisableCloseButton() command executes in a form setup to load upon the database initially loading. (Don't know how to do that? Email me.)

Disable Form Closing

  1. Set the form's properties as follows:
    Tab Property Setting
    Format Control Box No
      Close Button No
      Min/Max Buttons None
  2. Type the following code in the form's class module, in the declarations area:

    Public OKToClose As Boolean

    Note: you have to do this for EVERY form you want to have this effect on. If you want to save this step, you can instead type this in the declarations area of any of the modules in the database (certainly a good place would be in the mdlDisenableCloseButton created earlier in this example). (Yes, this is where I had to deviate from the book--or at least clarify what they meant in the book.)

  3. In the form's OPEN or LOAD event procedure, set the value of OKToClose to False, as follows:

    OKToClose=False

  4. In the form's Unload event, check the value of OKToClose. If it's False, set the Cancel parameter to True, halting the form's closing:

    Cancel=Not OKToClose

  5. In the command button used to close the form (say, cmdExit), set OKToClose to True and close the form (telling the form to close will automatically trigger the UNLOAD event procedures of the form):

    OKToClose = True

    (of course, make sure to have the following command as well)
    DoCmd.Close
    etc etc etc

How to Reverse Titlebar Close Button Disabling

  • Open the module mdlDisenableCloseButton (assuming that's what you named it), type CTRL-G to open the IMMEDIATE window, and type

    Call EnableCloseButton()

    and press ENTER

  • Remove the option to have the disable close button command execute upon intial database loading.
  • From every form, remove the OKToClose = False command from each form's LOAD or OPEN event procedure, and also remove every Cancel=Not OKToClose command from every form's UNLOAD event procedure, as well as every OKToClose = True command from each form's command buttons used for exiting.