ABC Blocks


Database and Website Design, Development, and Deployment--All in the Service of Your Mission

Often, users want out of your database, and they want out fast. But there are good database closings and bad database closings. To avoid corruption and locking issues, it's best to make sure that all of your connections to the back end (data side) of the database are closed before closing Access. And there are often clean-up tasks that get performed in code when a database closes. So it is usually best to control how a user closes the database.

You can't stop a user from giving the old three-finger salute, hitting the power button, or kicking out the power cord. But you can disable the ability to close the database any way other than one you design, and give the user a good indication of how you want the database closed.

Yes, you can use API calls to disable the X. But it's such a standard piece of the Windows interface that we prefer to keep that visible. Instead, here's some code to trap the event if the user tries to close the database that way, by using the task manager, or by hitting Alt-F4. It sends up a message box asking the user to close all forms in the database and then close the database itself. In reality, all of our databases have a Quit button on the first form you see, and closing all other forms will open this form, so users will be able to click the Quit button and close the application in a controlled manner.

Here's how we go about it...

First, make one of your forms stay open the entire time the user is in the database. AlphaBet City Dataworks applications always have some sort of switchboard form (not built by a wizard, but it does the same task--gets the user to whatever function he or she wants), so we use that one. You can make the form invisible, if you want, but make it the first form opened and make sure it never gets closed.

In the Unload Event of this form, put code something like this:

Private Sub Form_Unload(Cancel As Integer) On Error GoTo Error If AllowExit = False Then Cancel = True End If Exit Sub Error: Call ErrorTrap (Err.Number, Err.Description, "Form Unload", "frmSwitchboard") End Sub

Here's the AllowExit function:

Public Function AllowExit() As Boolean On Error GoTo Error Dim sSql As String If Forms!frmswitchboard!cmdQuit.Tag = "allow exit" Then AllowExit = True Else AllowExit = False Call MsgBox ("To close this application, first close all forms and then click'Quit' on the Switchboard Form.", vbExclamation) End If Exit Function Error: Call ErrorTrap (Err.Number, Err.Description, "AllowExit") End Function

In the button you want users to use to close your database, put this code:

Private Sub cmdQuit_Click() On Error GoTo Error Call SomeRoutineToCleanUpYourData() Md!cmdQuit.Tag = "allow exit" Call db.Close Set db = Nothing Call Application.Quit Exit Sub Error: Call ErrorTrap (Err.Number, Err.Description, "cmdQuit Click", "frmSwitchboard") End Sub

That's all there is to it. Now, when a user tries to close the database by clicking on that lovely little X, he or she will get a polite message, with instructions on how to properly close the database.

The ErrorTrap function is documented elsewhere. You can just yank it or use your own.