Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Prevent Closing Access with the X by mistake

Status
Not open for further replies.

spitzmuller

Programmer
May 7, 2004
98
0
0
CH
Hi out there

Great Forum! But couldn't find an answer for this one...

I'd like to stop the closing process of access when a user hits the X in the upper-right corner of the application window. I'd like then to ask the user if he is sure and if he clicks yes close access, if he clicks no cancel the operation.

I found quite some stuff on disabling the X of the application window, but I don't want to be that drastic. After all, the X sometimes is the only responding thing left...

Any ideas?

Thanks in advance Simon
 
I do the following:

Define a variable to hold a boolean at the top of the form's code
Dim blnOKToClose As Boolean

Set the variable to false when the form opens
blnOKToClose = False

Using the Form Unload event check variable
If blnOKToClose = False Then
If MsgBox("Do you wish to close?", vbYesNo, "Close?") = vbNo Then
Cancel = True
End If
End If

And then if you want a "graceful" exit, such a your own Exit Button
blnOKToClose = True
DoCmd.Close

Let me know if you have any trouble with that...

=======================================
People think it must be fun to be a super genius, but they don't realize how hard it is to put up with all the idiots in the world. (Calvin from Calvin And Hobbs)

Robert L. Johnson III
CCNA, CCDA, MCSA, CNA, Net+, A+, CHDP
VB/Access Programmer
 
Hi mstrmage1768

Tanks for your reply. Your code works fine if a form window within access is closed with the "X"

But I am talking about the Access-application window itself (the mother window if you want). There is no UnLoad-Event for Access itself...

Simon
 
Simply use an hidden startup form ...

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ181-2886
 
As PHV stated, the same principal would apply. I missed the fact that you were talking about the Access MDI window itself. A hidden startup form would work, applying the same basic setup I stated.

=======================================
People think it must be fun to be a super genius, but they don't realize how hard it is to put up with all the idiots in the world. (Calvin from Calvin And Hobbs)

Robert L. Johnson III
CCNA, CCDA, MCSA, CNA, Net+, A+, CHDP
VB/Access Programmer
 
If you're really that concerned then why not disable the Application Close button, people can always close ACCESS by using the Task Manager if necessary.

PaulF
 
Hi PaulF

Well, maybe your users can, but my users definitely cannot do that (as in: they are total windows newbies). Otherwise yeah - you're right.
 
another thing to think of is that you can enable and disable the ACCESS Application Close button and enable it again without leaving the database. Therefore, if you are only concerned with them leaving a form that is used to add/edit records, you can disable the Close button when the form opens, and then enable it again only if the form closes as it should via a command button. There are examples on this site of how do enable/disable the ACCESS Application Close button and also how to check to ensure the form is being closed as it should be.

PaulF
 
Thanks for all your help so far. But it still doesn't work.

I created a hidden form and i open it from the Form_Open-Routine of my Main Window.

When the hidden form's unload function is called, i can set

Chancel = True

as much as I wan't, I can't prevent the Database from closing.

I guess I am missing something... is it possible that the hidden form MUST be opened with an AUTOEXEC-Macro for this trick to work?

Thanks again in advance Simon
 
Simon,

Sorry for this being so difficult, but I am glad to see you sticking it out. Here's what I did in a test database which worked for me.

I created Form1...in the OnOpen Event of Form1 I have the following:
Code:
DoCmd.OpenForm "Form2", , , , , acHidden

I then created Form2. My Form2 has the following code:
Code:
Public blnOKToClose As Boolean

Private Sub Form_Open(Cancel As Integer)

blnOKToClose = False

End Sub

Private Sub Form_Unload(Cancel As Integer)

If Not blnOKToClose Then
    If MsgBox("Close?", vbYesNo, "Close") = vbNo Then
        Cancel = True
    End If
End If

End Sub

I saved all this and then opened Form1. I hit the ACCESS x in the top right and saw a message box that asked me to close. If I said no, Form1 remained visible. If I said yes, the whole database closed down.

Hope this helps you find a solution...Let me know if I can help more.

=======================================
People think it must be fun to be a super genius, but they don't realize how hard it is to put up with all the idiots in the world. (Calvin from Calvin And Hobbs)

Robert L. Johnson III
CCNA, CCDA, MCSA, CNA, Net+, A+, CHDP
VB/Access Programmer
 
Here's an example of tying the code to the specific form that you use to add/edit data. This example disables the ACCESS Application Close Button when it opens, and turns it back on when it is closed using the command button. If you attempt to close the form without using the command button the Unload event checks for the value of a module level variable and cancels the event. Be sure to change the name of the Sub tied to the command button to match the name of the command button in your form.

However, this can still be overridden by the Task Manager.

first add this to a Module

'Calvin Smith
''Tip # 19
'**********************************************************
'************ Code provided by CodeDisk II ****************
'**********************************************************

'We need the following API declarations first

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

Private Declare Function apiGetSystemMenu Lib "user32" Alias _
"GetSystemMenu" (ByVal hwnd As Long, ByVal flag As Long) _
As Long
Const MF_BYCOMMAND = &H0&
Const MF_DISABLED = &H2&
Const MF_ENABLED = &H0&
Const MF_GRAYED = &H1&
Const SC_CLOSE = &HF060&

Function EnableDisableControlBox(bEnable As Boolean, _
Optional ByVal lhWndTarget As Long = 0) As Long

On Error GoTo ErrorHandling_Err

' ----------------------------------------------------------------------
' Purpose: Example of how to disable or enable the control box of
' a form, report, or the Access parent window.
'
' Accepts: bEnable, which determines whether to disable or enable
' the control box
'
' Also accepts lhWndTarget (which is optional), if you want
' to use a window handle other than the Access parent window.
'
' Returns: N/A
'
' Example usage: lRetVal = EnableDisableControlBox(True) to enable -OR-
' lRetVal = EnableDisableControlBox(False) to disable
'
' NOTE: If no hWnd is passed in for a specific form, then the code
' assumes you want to enable/disable the Access parent window
' ----------------------------------------------------------------------
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

EnableDisableControlBox = lReturnVal

ErrorHandling_Err:
If Err Then
'Trap your error(s) here, if any!
End If
End Function




Next add this to the Form you want to disable the Close button:

Option Compare Database
Option Explicit
Dim blnOktoClose As Boolean

Private Sub cmdCloseAndEnable_Click()
blnOktoClose = True
Dim lRetVal As Variant
lRetVal = EnableDisableControlBox(True)
DoCmd.Close acForm, Me.Name
End Sub

Private Sub Form_Load()
Dim lRetVal As Variant
lRetVal = EnableDisableControlBox(False)
End Sub

Private Sub Form_Unload(Cancel As Integer)
If blnOktoClose = False Then
MsgBox "You Can Only Close This Form By Using The Command Button"
Cancel = True
End If
End Sub



PaulF
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top