Forcing a user to close database with command button

Henley12

Troy University Fan
Local time
Today, 04:10
Joined
Oct 10, 2007
Messages
222
Is there a way to force users to close a database using only a command button on a form?
 
Do you mean having the users push the button or something else?
 
I have a main form with an Exit Database button that has code behind it. If someone closes the database with the X at the top of the database, the code does not get executed.
 
Oh - Yeah I think you can by tinkering with an API call and disable that button but it escapes me at the moment. Maybe some else can jump in...
 
I'm trying to force users to close the database by pressing the Exit Database button instead of closing out of the program with the X.
 
I think missinglinq knows that, but what she was after is what code do you need executed so we'd know if it can be solved other way short of tinkering with the API as KenHigg mentioned. I'm not so keen on disabling the "X" because it may just confuse the users and make them give your application the three-finger salute, which is downright worse.

For example, if you have a switchboard, you could just hide it when another form is open and use its Close event to execute whatever needed to be execute.
 
My point exactly, Banana! BTW, I have both an X and a Y chromosome!
 
Doh! I had in my mind that it was "Miss Linq" or something like that.... bad, bad, bad Banana!

:o
 
do you guys see a problem with "disabling" the X button by means other than the API? users often try to shut down access without closing open objects etc. how about using a hidden form? if the user hits the X button and the hidden form is open (which it always is, from startup) then access doesn't close, forcing the user to exit via a main form?
 
I remember there were issues, but this escapes me.

I know there was a quite lengthy thread discussing this a way back (1-2 months ago), and IIRC, it involved myself, Bob Larson, and Ken Higg. I just can't remember the title of that thread...

We also did discuss the hidden form thingy, but I remember thinking that it sounded far too troublesome...
 
I have a main form with an Exit Database button that has code behind it. If someone closes the database with the X at the top of the database, the code does not get executed.

Why dont you just set the 'CLOSE BUTTON' to 'NO' on the format tab of the form that you described above?

Or altenatively

Paste the code form you 'Exit database button' into the 'On Close event' of the same form?

or have I missed something in this post?
 
i think henley is talking about the close button of Access, the application, not the form.
 
Well if thats the case, If he doesn't want the op to use the close X button then I presume that he wouldnt mind hiding the Access toolbars as well.

Set the 'CLOSE BUTTON' property to 'NO'
Set the 'POP UP' property of the first form to 'YES'
Set the 'Moveable' property of the first form that opens to 'NO'
Maximize the form on load

and at the end of his 'Exit ' button code paste...

DoCmd.RunCommand acCmdExit

This will hide everything so the op has to use the 'Exit' button.
 
Pretty sure this won't stop even a modal form....
 
well!
amazing how the right combination of things, in the right order, will give you something unexpected. like setting 'locked' and 'enabled' in just the right order ...
tnx for that.
 
Last edited:
In the Form which contains your Exit Database button, place the following code into the Forms' OnUnload event. It should look like:

Code:
Private Sub Form_Unload(Cancel As Integer)
   MsgBox "Please Close This Database By Using The" & vbCr & _
          "QUIT Button Located On Main Form.", vbCritical, _
          "Illegal Close"
   Cancel = True
End Sub

In general...I find this usually effective enough. Do keep in mind though, you won't be able to even place the Form into Design View. You will need to reopen the Database, and if your Main Form is the Startup Form then reopen it with the Shift Key held down.

Even trying to close the Database ALT-F4 or through the Task Manager (CTRL-ALT-DEL) will still display the message box and cancel the close but Access will shortly respond with a "Not Responding" message and allow the application to close if End Task is selected from this message (from Task Manager only). There are of course Windows API functions to hide an application from the Task Manager but then more code is required to carry out that task.

A CTRL-Break while the Message Box is displayed will also bypass the above method unless disabled.

.
 
try this as a start - this in an unload event of the switchboard etc, will popup under ANY circumstances - even if they just try to X out of Access

Code:
Private Sub Form_Unload(Cancel As Integer)
    If MsgBox("Do you really want to exit " & myappname, vbYesNo + vbQuestion + vbDefaultButton2, "Confirm Exit") = vbNo Then
        Cancel = vbCancel
    End If
        
End Sub
 

Users who are viewing this thread

Back
Top Bottom