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.
One technique 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.
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.
It turns out that this code does not work with the original version of MS Access 2007 on Windows XP. Running the same code in an earlier version of Microsoft 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 the SP2 version of Access 2007, this now works.
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.
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
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
' Params : pfEnabled TRUE enables the close button, FALSE disabled it
' Owner : Copyright (c) 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
lngFlags = clngMF_ByCommand Or clngMF_Grayed
Call EnableMenuItem(lngMenu, clngSC_Close, lngFlags)
When your application starts, simply call this to disable the close button:
You'll see that the MS Access close button is now disabled:
The user can only exit the program through your application.
When your application ends, simply call this to enable the close button:
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.
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!