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 bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to the close form action?

Status
Not open for further replies.

Doraemon

IS-IT--Management
Sep 12, 2003
31
HK
I have designed a form in which when the user clicks the "Close" button in the top right hand corner, a msgbox will popped up. If user clicks "Cancel" in the msgbox, the form will NOT be closed.

As shown below, I have tried: Me.Undo
However, it doesn't work.

Private Sub Form_Close()
On Error GoTo Err_form_close

Dim response
response = MsgBox("Do you want to close the form without saving current records?", vbOKCancel, "Notics")

If response = vbOK Then
...
ElseIf response = vbCancel Then
Me.Undo <-- WRONG
End If
End Sub

Can anyone tell me how can I return to the form instead of closing it, if the user clicks &quot;Cancel&quot; in the msgbox?
 
Thanks for your help.
But the form is still closed unexpectedly.
 
Oops, sorry. The code should be placed in the Unload event, not the Close event. And while DoCmd.CancelEvent will work, all events that can be canceled include &quot;Cancel As Integer&quot; as an argument, so you can use Cancel = True instead of DoCmd.CancelEvent. Like so:

Code:
Private Sub Form_Unload(Cancel As Integer)

If MsgBox(&quot;Do you want to close the form without saving current records?&quot;, vbOKCancel, &quot;Notice&quot;) = vbCancel Then
     Cancel = True
     Else
          ... other code
End If
End Sub

Ken S.
 
Yes, you are right!
I should put the codes for msgbox and do the checking for the msgbox input in the Form_Unload() event instead of Form_Close() event to allow the user return to the form if they click cancel in the msgbox.

Really thx!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top