How to prevent a form from closing

B

Ben

Hi all,

Access 2003. I have a form, but I do not want the use to close, instead I
provided the button to go back to the previous form. If the user clicks on
the close button (x), I would like to add the logic to ask if the user really
wants to close, if not they clicked no, then do nothing.

In the form's Close event, in put in an IF statement:

Choice = MsgBox("Are you sure you want to quit the application?", vbYesNo +
vbCritical + vbDefaultButton2)
If Choice = vbYes Then
DoCmd.OpenForm "PreviousForm"
Else

' here is where I am not sure what to write
'I want to force the current form to stay opened if user choose no

End If


Thanks for your thoughts.

Ben


--
 
D

Douglas J. Steele

While it may seem counterintuitive, you have to use the form's Unload
event, not its Close event.

Private Sub Form_Unload(Cancel As Integer)

Dim Choice As Long

Choice = MsgBox("Are you sure you want to quit the application?", _
vbYesNo + vbCritical + vbDefaultButton2)
If Choice = vbYes Then
DoCmd.OpenForm "PreviousForm"
Else
Cancel = True
End If

End Sub
 
B

Ben

Doug,

Works great!

Thanks,

Ben

--



Douglas J. Steele said:
While it may seem counterintuitive, you have to use the form's Unload
event, not its Close event.

Private Sub Form_Unload(Cancel As Integer)

Dim Choice As Long

Choice = MsgBox("Are you sure you want to quit the application?", _
vbYesNo + vbCritical + vbDefaultButton2)
If Choice = vbYes Then
DoCmd.OpenForm "PreviousForm"
Else
Cancel = True
End If

End Sub
 

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