Form on close

G

Guest

What code do I need if the following is an event for On Close of a form if I
want the message to appear but then allow the form to remain open when they
click OK.

Private Sub Form_Close()

If MsgBox("Make sure DATE CORRESPONDENCE FIELD is completed when closing out
this service call for the final time!", vbOKCancel) = vbOK Then

End If

End Sub
 
D

Dirk Goldgar

bdehning said:
What code do I need if the following is an event for On Close of a
form if I want the message to appear but then allow the form to
remain open when they click OK.

Private Sub Form_Close()

If MsgBox("Make sure DATE CORRESPONDENCE FIELD is completed when
closing out this service call for the final time!", vbOKCancel) =
vbOK Then

End If

End Sub

You can't do it in the form's Close event -- that event cannot be
cancelled, so once it fires the form is going to close no matter what
you do. Use the form's Unload event instead. The Unload event fires
first, and it can be cancelled using the Cancel argument of its event
procedure:

'----- start of revised code -----
Private Sub Form_Unload(Cancel As Integer)

If MsgBox( _
"Make sure DATE CORRESPONDENCE FIELD is " & _
"completed when closing out this service call for " & _
"the final time!", _
vbOKCancel) _
= vbOK _
Then
Cancel = True
End If

End Sub

'----- end of revised code -----

I must say, though, I'd rather expect you to cancel the form's closing
if the user clicked the Cancel button rather than the OK button.
 
J

James Hahn

Use the unload event, and cancel it.
Private Sub Form_Unload(Cancel As Integer)
If MsgBox("Close form?", vbYesNo) = vbYes Then
Exit Sub
Else
Cancel = True
End If
What code do I
need if the following is an event for On Close of a form if I
 

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