Disable Application Close Button in Access 2003

K

KenR

I need to force users to exit using the button I provided, as several
housekeeping chores get bypassed if users exit using the "X" button in the
upper right corner of the application window. How do I disable this button?

I saw a previous post:

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

'Disable/Enable the Close Button Option
Public Sub CloseButtonState(boolClose As Boolean)
Dim hWnd As Long
Dim wFlags As Long
Dim hMenu As Long
Dim Result As Long

hWnd = Application.hWndAccessApp
hMenu = GetSystemMenu(hWnd, 0)
If Not boolClose Then
wFlags = MF_BYCOMMAND Or MF_GRAYED
Else
wFlags = MF_BYCOMMAND And Not MF_GRAYED
End If

Result = EnableMenuItem(hMenu, SC_CLOSE, wFlags)
End Sub

Then in your code, maybe in a splash form:
Call CloseButtonState(False) 'Disables X
Call CloseButtonState(True) 'Enables X
***********************

but I get a "Variable not Defined" error when it is called. Where are the
variables MF_BYCOMMAND and MF_GRAYED declared?

Thanks for any help...
 
D

Dirk Goldgar

KenR said:
I need to force users to exit using the button I provided, as several
housekeeping chores get bypassed if users exit using the "X" button in the
upper right corner of the application window. How do I disable this
button?

I saw a previous post:

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

'Disable/Enable the Close Button Option
Public Sub CloseButtonState(boolClose As Boolean)
Dim hWnd As Long
Dim wFlags As Long
Dim hMenu As Long
Dim Result As Long

hWnd = Application.hWndAccessApp
hMenu = GetSystemMenu(hWnd, 0)
If Not boolClose Then
wFlags = MF_BYCOMMAND Or MF_GRAYED
Else
wFlags = MF_BYCOMMAND And Not MF_GRAYED
End If

Result = EnableMenuItem(hMenu, SC_CLOSE, wFlags)
End Sub

Then in your code, maybe in a splash form:
Call CloseButtonState(False) 'Disables X
Call CloseButtonState(True) 'Enables X
***********************

but I get a "Variable not Defined" error when it is called. Where are the
variables MF_BYCOMMAND and MF_GRAYED declared?

Thanks for any help...


They're probably declared in the Declarations section of the module you
copied that from, and you probably omitted to copy them. However ...

The more common way to handle this is to open a hidden form at startup, and
use the Unload event of that form either to do your housekeeping tasks or
else to cancel the unload, and hence the closing of Access, unless you can
determine that the user did click your Exit button.
 
K

KenR

I didn't copy the code from a module, it was posted, in its entirety, in this
forum.

However, I'm happy to use a different method... whatever will work

I already have a hidden form running at all times in the database, so I can
include any necessary code in the form's unload event. The housekeeping
tasks I need to perform may require some user involvement, so I can't just
attach the tasks to the event.

I would like to cancel the close event if the user didn't exit using the
button I provided. However, I don't know how to "cancel the unload," as you
referred to it. Can you help with that?

thanks

Ken
 
A

Armen Stein

The more common way to handle this is to open a hidden form at startup, and
use the Unload event of that form either to do your housekeeping tasks or
else to cancel the unload, and hence the closing of Access, unless you can
determine that the user did click your Exit button.

It isn't a good practice to use Globals except only momentarily, but
just in case you are:

Remember that if you cancel the Unload event during a quit, any Global
variables have already been cleared out.

Armen Stein
Microsoft Access MVP
www.JStreetTech.com
 
D

Dirk Goldgar

KenR said:
I didn't copy the code from a module, it was posted, in its entirety, in
this
forum.

However, I'm happy to use a different method... whatever will work

I already have a hidden form running at all times in the database, so I
can
include any necessary code in the form's unload event. The housekeeping
tasks I need to perform may require some user involvement, so I can't just
attach the tasks to the event.

I would like to cancel the close event if the user didn't exit using the
button I provided. However, I don't know how to "cancel the unload," as
you
referred to it. Can you help with that?

Here's a way to manage it: Put an unbound checkbox control named
"chkOkayToClose" on the hidden form, with the default value False. In your
Exit command button's Click event, after you've taken care of your
housekeeping but before issuing your Quit command, add the line

Forms!YourHiddenFormName!chkOkayToClose = True

(changing the form name appropriately). In the Unload event of your hidden
form, put an event procedure like this:

'----- start of code -----
Private Sub Form_Unload(Cancel As Integer)

If Me!chkOkayToClose = False Then

MsgBox _
"Please click the Exit button to close this application.", _
vbExclamation, _
"Exit Using Button"

Cancel = True

End If

End Sub
'----- end of code -----

That ought to do it. Bear in mind Armen Stein's warning about global
variables. By the time you get to the hidden form's Unload event, they will
probably already have been cleared.
 

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