close button upper right corner (X)

J

Jose Perdigao

When I click on the button upper right corner, the access application close.
I would like create a function to do the follows:
If I click on this button, must show a message to ask the user if do you
really want quit the application, if the response is yes, then close, if is
no, doesn't close.

is it possible?

anyone can help me?

thanks,
jcp
 
S

Stuart McCall

Jose Perdigao said:
When I click on the button upper right corner, the access application
close.
I would like create a function to do the follows:
If I click on this button, must show a message to ask the user if do you
really want quit the application, if the response is yes, then close, if
is
no, doesn't close.

is it possible?

anyone can help me?

thanks,
jcp

Better to just disable the button, then you don't have to bother the user
for confirmation. You need to provide them with a means to shut down the
app, of course. All you need is something clickable, like a button, and set
its OnClick to:

Application.Quit

Paste the following code into a standard module:

''' BEGIN CODE '''
Option Compare Database
Option Explicit
'
Private Const MF_BYPOSITION = &H400
Private Const MF_REMOVE = &H1000
'
Private Declare Function DrawMenuBar _
Lib "user32" (ByVal hWnd As Long) As Long
Private Declare Function GetMenuItemCount _
Lib "user32" (ByVal hMenu As Long) As Long
Private Declare Function GetSystemMenu _
Lib "user32" (ByVal hWnd As Long, _
ByVal bRevert As Long) As Long
Private Declare Function RemoveMenu _
Lib "user32" (ByVal hMenu As Long, _
ByVal nPosition As Long, _
ByVal wFlags As Long) As Long

Public Sub DisableAccessCloseButton()
Dim appHwnd&, mnuHwnd&, mnuCount&

appHwnd = Application.hWndAccessApp
mnuHwnd = GetSystemMenu(appHwnd, 0)
If mnuHwnd& Then
mnuCount = GetMenuItemCount(mnuHwnd)
'Remove Close
RemoveMenu mnuHwnd, mnuCount - 1, _
MF_REMOVE Or MF_BYPOSITION
'Remove separator line
RemoveMenu mnuHwnd, mnuCount - 2, _
MF_REMOVE Or MF_BYPOSITION
DrawMenuBar appHwnd
End If
End Sub
''' END CODE '''

Then call it when your app starts up.

Note that this will work for all 32 bit versions of Access, except Access
2007 (working on that one).
 
J

Jose Perdigao

Thanks Stuart,
I tested in acc2003 it works perfectly.
I acc2007 doesn't work.

I don't like to see the button disable, I prefer generate a message and then
if the user wants to quit, click yes.
Is it possible from your code change something to enable a button and
generate a message for the user choose quit or not?

If you finish the code for acc 2007, let me know.

Thanks a lot
jcp
 
S

Stuart McCall

Comments inline:

Jose Perdigao said:
Thanks Stuart,
I tested in acc2003 it works perfectly.
I acc2007 doesn't work.

I don't like to see the button disable, I prefer generate a message and
then
if the user wants to quit, click yes.
Is it possible from your code change something to enable a button and
generate a message for the user choose quit or not?

The Access window's close button doesn't generate an event, so there's no
way of catching it without the use of subclassing, which is too bothersome
in Access (it causes Access to crash at the slightest excuse - been there,
done that, won't do it again).

The problem with leaving the button enabled is that if you have some routine
that needs to be executed on app shutdown, when the user clicks this button
your code won't run.

If however you disable it and provide another means of closing the app, you
could ask the user for confirmation at that point (amongst other things).
If you finish the code for acc 2007, let me know.

At this point I'm not sure its possible. The Access 2007 window isn't
responding to code as it should (MS breaking their own rules _again_).
However, if I succeed (or spot someone else's solution) I'll keep you
posted.
 
P

Pieter Wijnen

The easist way to overcome is to place the code in the Form_Unload Event of
the Main (switchboard) Form

Pieter
 
S

Stuart McCall

"Pieter Wijnen"
The easist way to overcome is to place the code in the Form_Unload Event
of the Main (switchboard) Form

Pieter

Yes I'm aware of that (andI probably should have mentioned it to the OP).
However, some of my apps don't have a form that's guaranteed to be open for
the whole app's lifetime, which is why I created the module (actually I
filched some of the the code from somewhere and adapted it to my own purpose
:)
 
G

George Nicholson

The following doesn't allow the user a choice, they have to use a specific
"Exit App" button to close the app. You can modify the Form_Unload message
box and provide additional options if you wish.

Hope this points you in a fruitful direction.

1) Create a "always open" form. You can use an existing form (i.e.,
switchboard or similar) if appropriate. This form can be hidden. The key is
that once open, this form will *never* close until the app is ready to
close.

2) Add some code.
************************************
' (in the General declarations area of Form "frmAlwaysOpen")
Public gOkToClose as Boolean

'(in Form_Open for "frmAlwaysOpen")
Private Sub Form_Open(Cancel As Integer)
' Redundant, but included for clarity
gOKtoClose = False
End Sub

'(on Form_Unload for "frmAlwaysOpen")
Private Sub Form_Unload(Cancel As Integer)
' Cancel unless user has used the "Close App" button
If gOKtoClose Eqv False Then
MsgBox "You must use the 'Exit App' button to Exit the
Application.", vbOKOnly, "Can't Exit"
' You may need to do other things in the UI (reopen forms), provide
instructions, etc, at this point.
Cancel = True
Else
' Any "Run before app closes" code could go here
End If
End Sub


'(cmdExitApp button is the only way the user can exit the app.
' It does not have to on frmAlwaysOpen, but it can be.)
Private Sub cmdExitApp_Click()
'Uncomment *one* set of lines below

''If cmdExitApp is on a form other than frmAlwaysOpen
'Dim strFormName as string
'strFormName = "frmAlwaysOpen"
'Forms(strFormname).gOKtoClose = True
'DoCmd.Close acForm, strFormname, acSaveNo
'DoCmd.Quit

'' If cmdExitApp is on frmAlwaysOpen
'gOKtoClose = True
'DoCmd.Close acForm, Me.Name, acSaveNo
'DoCmd.Quit
End Sub
************************************

HTH,
 
J

Jose Perdigao

Thans stuar by your explanation.
I thought there was possible to generate a message.
Regards.
jcp
 
P

Pieter Wijnen

I always have a hidden form open to store "Public Variables" & to check
wether to kick users out to perform maintenance

Pieter
 

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