(Q) I want to make sure that users always exit my database using the Exit button
provided on my Switchboard. However, I'm not able to prevent them from closing Access
itself. Is there a way to control how they exit Access?
(A) You can use the UnLoad event of a form to do this. If your switchboard is the first
form to open and always remains open in the background, you can apply the following method
on the switchboard. Otherwise, I would recommend creating a hidden form for this
functionality.
Define a new variable in a new module
Public pboolCloseAccess as Boolean
'Note: Access 2.0 does NOT recognize Boolean
'type variables. Use integer type instead,
'passing a -1 as True, and 0 as False.
Now, when you open the database, set
pboolCloseAccess=False
In your (Switchboard/Hidden) Form's Unload Event, set the variable to true and also
check for its value
pboolCloseAccess=True
DoCmd.Close acForm, "hfrmCloseAccess"
docmd.Quit
If your Switchboard form closes during one instance, then create a new form
(hfrmCloseAccess). You can minimize the size if you want because it will be in hidden
mode. Type the following in hfrmCloseAccess's OnUnload Event
if not pboolCloseAccess then cancel = true
Create the Autoexec macro. If you already have one, make sure that you open
hfrmCloseAccess before anything else IN HIDDEN MODE.
The reasoning here is, when someone clicks X in Access's window, the System starts
closing down all open objects in a first-open-last-close basis. Since your hfrmCloseAccess
form is the first one to open, it will be the last to close. And since the boolean var is
still False, Cancel will always be true, hence Access will not close. On the other hand,
when the user clicks on the Exit button or closes your switchboard, you're setting the
boolean var to True and then closing the hfrmCloseAccess form, which WILL close now and
hence Access will close itself.
|