Cancel the Close Event

MarionD

Registered User.
Local time
Today, 21:10
Joined
Oct 10, 2000
Messages
425
Hi all,
I’m back again with another question! Thank heaven for … the Forum!

How can I - If the User clicks on the X in the Title Bar
1. Msgbox “Do you really want to close the DB”
2. Then Cancel the Close Event when the User says NO.

I’ve tried putting this on the last form to close – the msgbox works fine but even if I say NO (docmd.cancelEvent) the DB still closes.

The reason I want to do this, is that often the User wants to close a report or Form and ends up closing the DB.

Thanks for any help.
 
You want to use the Form_Unload(Cancel as Integer) and return a value of -1 to the Cancel parameter if your use indicates that the Close operation was in error.

You can cancel a FORM OPEN but not a FORM CLOSE. You can cancel a FORM UNLOAD but not a FORM LOAD.

Your trickier problem is trying to determine whether the UNLOAD is being caused because the form's X got clicked or because the database window's X got clicked.

One way to assure this (but it is tedious) is to add a button to EVERY form to close that one form. Then REMOVE the close button from the form's window. (Use {form-name}.CloseButton = False to disable this button.) Note that ALT+F4 still closes the form anyway. A savvy user could do that.

Now, if you take the click event on the button you added, you can set a variable in the form's general declaration area of its class module and then close the form (Me.Close). Now the FORM UNLOAD event can see the variable left behind by the click event.

If you get a close and it didn't come from the click event, then it must have been from someone closing the database. Or someone trying to shut down the workstation.

Another way to block this works if you have a persistent startup form that becomes invisible and minimized, but never actually closes until the DB itself is closing. You could put the code you mention in the Form_Unload event of that form (if you have such a form) and it would ONLY be triggered by closing the DB. That is because the invisible, minimized form is not capable of being selected so that you could selectively close it.
 
I prefer to remove the X in the applicaton title bar for more control over my applications.

This will allow you to disable the 'X' to prevent the user from closing your application...

'Copy this function into a new module...
'Courtesy of Calvin Smith

Option Compare Database
Option Explicit

Public Declare Function apiEnableMenuItem Lib "user32" Alias "EnableMenuItem" (ByVal hMenu As Long, ByVal wIDEnableMenuItem As Long, ByVal wEnable As Long) As Long
Public Declare Function apiGetSystemMenu Lib "user32" Alias "GetSystemMenu" (ByVal hWnd As Long, ByVal flag As Long) As Long

Public Function EnableDisableControlBoxX(bEnable As Boolean, Optional ByVal lhWndTarget As Long = 0) As Long
On Error GoTo Err_EnableDisableControlBoxX

Const MF_BYCOMMAND = &H0&
Const MF_DISABLED = &H2&
Const MF_ENABLED = &H0&
Const MF_GRAYED = &H1&
Const SC_CLOSE = &HF060&

Dim lhWndMenu As Long
Dim lReturnVal As Long
Dim lAction As Long

lhWndMenu = apiGetSystemMenu(IIf(lhWndTarget = 0, Application.hWndAccessApp, lhWndTarget), False)

If lhWndMenu <> 0 Then
If bEnable Then
lAction = MF_BYCOMMAND Or MF_ENABLED
Else
lAction = MF_BYCOMMAND Or MF_DISABLED Or MF_GRAYED
End If
lReturnVal = apiEnableMenuItem(lhWndMenu, SC_CLOSE, lAction)
End If

EnableDisableControlBoxX = lReturnVal

Exit_EnableDisableControlBoxX:
Exit Function

Err_EnableDisableControlBoxX:
ErrorMsg Err.Number & " - " & Err.Description
Resume Exit_EnableDisableControlBoxX

End Function

'I test if the current user is me (the programmer). Use this in a forms OnOpen event or a transparent command button...
If CurrentUser <> "programmer" Then
EnableDisableControlBoxX (False) 'Disable the programs X (close) function
Exit Sub
Else
EnableDisableControlBoxX (True)
End If

'HTH
 
Thank you Doc Man and GHudson for the replies. You guys are really life-savers!

I'll try both - I would prefer to keep the x on the Title Bar as it seems "Windows Normal" in all applications.

Thanks so much again - will let you know how I get on

Marion
 

Users who are viewing this thread

Back
Top Bottom