ABCDataworks
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:
Here's the AllowExit function:
In the button you want users to use to close your database, put this code:
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.