Jump: Search for: 

Free Resources from FMS

 

Thank you! Thank you! I just finished reading this document, which was part of a link in the recent Buzz newsletter. I have printed it for others to read, especially those skeptical on the powers of Access and its capabilities.

Darren D.

 

 

 

 

Disable the Microsoft Access Close Button to Prevent Users from Accidentally Closing Your Application

Provided by: Luke Chung, President of FMS, Inc.

Accidentally Closing the Access Database Application

Microsoft Access offers all sorts of events on forms and reports so you can control what the user does, and prevent mistakes such as users closing the form when you don't want them.

However,  Microsoft Access doesn't offer a Close Event for the program itself. The result is users accidentally closing Access when they shouldn't.

I've found myself guilty of this when I think I'm closing a table, form, or report and accidentally close Access. What a pain! And it can be particularly troublesome if you're modifying data or form/report designs.

This often happens when users are previewing a maximized report and confusing the [X] for closing the report with the [X] for closing Access:

Most users learn the hard way that the big [X] isn't what they want. It's not an intuitive design.

It's one thing when a developer makes this mistake, but quite a problem if your users have to experience it, then learn, "Don't do that". Clearly, that's not an acceptable solution.

Invisible Form Approach and its Limitations

One technique (hack) for preventing this is the use of a hidden form to trap for its OnClose event. This form is opened when the application starts and when it's closed unexpectedly, cancels the Close event and thereby prevents Access from closing. The problem with this is that it's triggered once the Access' close process starts, so other objects in the application may have closed already before this form closes which may leave the user in an inconsistent state.

What Should Really Exist

Ideally, Microsoft Access would offer an Access Close event that you could control and prevent. Unfortunately that doesn't exist. Seems rather ironic that the minor close events of individual objects can be trapped but the big one is not trappable.

Barring the Microsoft Access development team addressing this, wouldn't it be ideal to simply disable to close button? Fortunately, you can.

Issues with Access 2007 and Windows XP

It turns out that this code does not work with the original version of Access 2007 on Windows XP. Running the same code in an earlier version of Access on the same Windows XP machine works fine. And running the code in Access 2007 with Windows Vista or Windows 7 also works fine. With he SP2 version of Access 2007, this now works.

Code to Disable the Close Button

This example comes from Total Visual SourceBook, our royalty-free code library. The code is from module modAccessWindow in the Access, Environment category. The module also includes code to manage the control box, and the transparency and opacity of form windows.

API Calls

There are two Windows API calls to use. Add these to the declarations section of your module:

Private Declare Function GetSystemMenu Lib "user32" (ByVal hwnd As Long, ByVal wRevert As Long) As Long

Private Declare Function EnableMenuItem Lib "user32" (ByVal hMenu As Long, ByVal wIDEnableItem As Long, ByVal wEnable As Long) As Long

Procedure to Disable and Enable the Close Button

Create this procedure:

Public Sub AccessCloseButtonEnabled(pfEnabled As Boolean)
  ' Comments: Control the Access close button.
  '           Disabling it forces the user to exit within the application
  ' In      : pfEnabled       TRUE enables the close button, FALSE disabled it
  ' Owner   : Copyright (c) 2008-2009 from FMS, Inc.
  ' Source  : Total Visual SourceBook
  ' Usage   : Permission granted to subscribers of the FMS Newsletter

  On Error Resume Next

  Const clngMF_ByCommand As Long = &H0&
  Const clngMF_Grayed As Long = &H1&
  Const clngSC_Close As Long = &HF060&

  Dim lngWindow As Long
  Dim lngMenu As Long
  Dim lngFlags As Long

  lngWindow = Application.hWndAccessApp
  lngMenu = GetSystemMenu(lngWindow, 0)
  If pfEnabled Then
    lngFlags = clngMF_ByCommand And Not clngMF_Grayed
  Else
    lngFlags = clngMF_ByCommand Or clngMF_Grayed
  End If
  Call EnableMenuItem(lngMenu, clngSC_Close, lngFlags)
End Sub

Disabling the Close Button

When your application starts, simply call this to disable the close button:

  Call AccessCloseButtonEnabled(False)

You'll see that the MS Access close button is now disabled:

The user can only exit the program through your application.

Enabling the Close Button

When your application ends, simply call this to enable the close button:

  Call AccessCloseButtonEnabled(True)

Caution: Need to Manage Errors Properly

Be careful what you ask for! Now that your users cannot exit your program by closing Access directly, you need to make sure your application doesn't get stuck in an infinite loop or dead-end (where there's no exit). You'll also need to properly handle errors and exit gracefully if there's a problem. If not, your users will be forced to use Task Manager to close your instance of Access. Not good.

Additional Resources

To ensure you handle unexpected errors properly, make sure you have error handling throughout your application with a global error handler routine. Additional resources to help you are:

Hope this helps. Good luck!

Main tips page

Feedback

Contact Us  l   Web questions: Webmaster   l   Copyright © 2010 FMS, Inc., Vienna, Virginia
Celebrating 24 Years of Software Excellence