Microsoft Access Developer Center

Table Design

Query Design

Form Design

Form Tips and Mistakes

Module VBA to Forms and Controls

Form Navigation Caption

Using a RecordsetClone

Synchronize Two Subforms

Multiple OpenArgs Values

Late Bind Tab Subforms

Subform Reference to Control Rather than Field

Tab Page Reference

Shortcut Keys


Combo Box Top Tips

Properties and Validation

Select First Item

Cascading Combo Boxes

Zip, City, State AutoFill

Report Design

Suppressing Page Headers and Footers on the First Page of Your Report

Add the NoData Event

Annual Monthly Crosstab Columns

Design Environment

Adding Buttons to the Quick Access Toolbar

Collapsing the Office Ribbon for more space

VBA Programming

Basics: Forms and Controls

Using Nz() to Handle Nulls

Avoiding Exits in the Body of a Procedure

Shortcut Debugging Keys

Setting Module Options

Math Rounding Issues

Rename a File or Folder

Avoid DoEvents in Loops

Age Calculations

Weekday Math

Sending Emails with DoCmd.SendObject

Source Code Library

Microsoft Access Modules Library

Microsoft Access Modules

VBA Error Handling

Error Handling and Debugging Techniques

Error Number and Description Reference

Basic Error Handling

Pinpointing the Error Line

Performance Tips

Linked Database

Subdatasheet Name

Visual SourceSafe

Deployment

Runtime Downloads

Simulating Runtime

Prevent Close Box

Disable Design Changes

Broken References

Remote Desktop Connection Setup

Terminal Services and RemoteApp Deployment

Reboot Remote Desktop

Missing Package & Deployment Wizard

Avoid Program Files Folder

Microsoft Access Front-End Deployment

System Admin

Disaster Recovery Plan

Compact Database

Compact on Close

Database Corruption

Class Not Registered Run-time Error -2147221164

Inconsistent Compile Error

Decompile Database

Bad DLL Calling Convention

Error 3045: Could Not Use

Converting ACCDB to MDB

SQL Server Upsizing

Microsoft Access to SQL Server Upsizing Center

Microsoft Access to SQL Server Upsizing Center

When and How to Upsize Access to SQL Server

SQL Server Express Versions and Downloads

Cloud and Azure

Cloud Implications

MS Access and SQL Azure

Deploying MS Access Linked to SQL Azure

Visual Studio LightSwitch

LightSwitch Introduction

Comparison Matrix

Additional Resources

Microsoft Access Help

MS Access Developer Programming

More Microsoft Access Tips

Technical Papers

Microsoft Access Tools

Connect with Us

Email NewsletterEmail Newsletter

FMS Development Team BlogDeveloper Team Blog

Facebook PageFacebook (Feed)

Twitter with FMSTwitter

FMS Support SiteSupport Forum

 

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:

Close Microsoft 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 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 Microsoft Access 2007 and Windows XP

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.

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.

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
  ' 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
  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:

Close button

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!