Disable Database Close

K

Ken

How can I disable the red X in the upper right hand corner of the database?
Users sometimes accidentally close the database when they meant to only close
a form or report. I'd like to make them start using File -> Exit to really
exit. Thanks.
 
J

Jeff Boyce

Ken

So, you want to replace a single-click option (the "x") with a two-click
option (File, Exit)?! Your users must love you!

What about replacing one-click with one-click? If you add a command button
that runs the <Quit> command, that would be a single-click way to exit the
application. And then you could do away with those troublesome
menus/toolbars/ribbons that let the users get into things they really
shouldn't be messing with!

Good luck!

Regards

Jeff Boyce
Microsoft Office/Access MVP
 
D

Dirk Goldgar

Ken said:
How can I disable the red X in the upper right hand corner of the
database?
Users sometimes accidentally close the database when they meant to only
close
a form or report. I'd like to make them start using File -> Exit to
really
exit. Thanks.


I don't think you can do it that way, because the only technique I know of
for preventing unintended closing via the X button would also prevent
closing via File -> Exit (unless you provide your own menu bar and override
that command with your own code).

But if you give them a command button on a form, or your own toolbar button
to close Access, you can prevent Access from closing via the X *or* File ->
Exit. To do that, you would open a hidden, unbound form at startup, and put
a checkbox on it, "chkOkayToExit", with default value of False. Your custom
Exit button's code would set that check box to True, and then issue the
command DoCmd.Quit. The hidden form's Unload event procedure would check
the value of the check box, and cancel the Unload event if it is not set to
True. Cancelling the form's Unload event would prevent the form from
closing and hence prevent Access from closing.
 
K

Ken

Jeff and Dirk,

Thanks for your replies. Actually, all I'm looking for is a way to disable
MS Access from closing via the red X. Users are accidentally clicking on
this button thinking they're only closing a report. The funny thing is that
I did this in one of my applications several years ago but can't remember how
I did it.

Ken
 
K

Ken

I found it. This is called when the database loads:

Function InitApplication()
Dim c As CloseCommand
Set c = New CloseCommand

'Disable Close menu.
c.Enabled = False
End Function
 
D

Dirk Goldgar

Ken said:
I found it. This is called when the database loads:

Function InitApplication()
Dim c As CloseCommand
Set c = New CloseCommand

'Disable Close menu.
c.Enabled = False
End Function


Interesting! CloseCommand isn't built into Access. I think you probably
got it from this Knowlegebase article:

http://support.microsoft.com/kb/245746
ACC2000: How to Disable the Close Button (X)
on the Access Application Window

For any lurkers: the article shows how to create a class module named
CloseCommand that uses Windows API calls to disable the "X" button on the
Access application window. I just tried it out and it works.

It does not disable the File -> Exit menu item, so it won't preven the user
from closing Access that way. If one's goal is to prevent the user from
exiting the application without running some special termination code, this
won't do it. But it seems to me like a handy way to prevent inadvertently
clicking on the application's X button when, for example, forms are opened
in a maximized state. I've run into that problem from time to time, so I'm
glad to ler of this technique.

Thanks, Ken!
 
S

Stuart McCall

Dirk Goldgar said:
Interesting! CloseCommand isn't built into Access. I think you probably
got it from this Knowlegebase article:

http://support.microsoft.com/kb/245746
ACC2000: How to Disable the Close Button (X)
on the Access Application Window

For any lurkers: the article shows how to create a class module named
CloseCommand that uses Windows API calls to disable the "X" button on the
Access application window. I just tried it out and it works.

It does not disable the File -> Exit menu item, so it won't preven the
user from closing Access that way. If one's goal is to prevent the user
from exiting the application without running some special termination
code, this won't do it. But it seems to me like a handy way to prevent
inadvertently clicking on the application's X button when, for example,
forms are opened in a maximized state. I've run into that problem from
time to time, so I'm glad to ler of this technique.

Thanks, Ken!

--
Dirk Goldgar, MS Access MVP
www.datagnostics.com

(please reply to the newsgroup)

I'm also glad to learn this. Thanks for the heads-up. Although MS's code is
always a bit long winded for me. Here's how I'd use it:

Private Declare Function GetSystemMenu Lib "user32" (ByVal hWnd As Long, _
ByVal bRevert As Long) As Long

Private Declare Function EnableMenuItem Lib "user32" (ByVal hMenu As _
Long, ByVal wIDEnableItem As Long, ByVal wEnable As Long) As Long

Const MF_GRAYED = &H1&
Const MF_BYCOMMAND = &H0&
Const SC_CLOSE = &HF060&

Public Sub DisableAccessAppCloseButton()
Dim hMenu As Long

hMenu = GetSystemMenu(Application.hWndAccessApp, 0)
EnableMenuItem hMenu, SC_CLOSE, MF_BYCOMMAND Or MF_GRAYED
End Sub

Notice I've removed the code for re-enabling the button - don't see there's
a need for it (as you say there's still File->Exit).

Also this runs in a standard module. No need for instantiating classes etc.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top