CancelEvent

  • Thread starter Thread starter Geoff
  • Start date Start date
G

Geoff

I have a MsgBox popup which tells the user that the record has not been
saved on an unbound form if they try to close the form without posting. I
was hoping to use the CancelEvent function to stop the form from closing if
they picked cancel except I get a Run-time Error 2001 "You Canceled the
previous operation" which I don't want to appear. I've tried On Error Resume
Next and Application.DoCmd.SetWarning False but I can't stop the dialog from
popping up. Any suggestions?
 
Post the code that you're using to pop up the message box. Most likely,
you'll need to include a step like this in the code:

Response = acDataErrContinue

But exactly where you need to add that step depends upon the code and in
which event you're running the code. Is the user clicking one of your
command buttons for "cancel"?
 
I've put this in the Forms.OnUnload event

If (Me!Posted <> True Or IsNull(Me!Posted)) And Me!AddRec = True Then
If MsgBox("Your Changes will NOT BE SAVED" & Chr(10) & Chr(10) & _
"You must Post this Invoice to Save Changes", vbOKCancel, "Changes Not
Saved") = vbCancel Then
Application.DoCmd.SetWarnings False
DoCmd.CancelEvent
Else
blah blah blah...
End if
End if
 
Hi Geoff,

I'm assuming that the code below is in the Form_Unload event of the form
itself. The Form_Unload event has a single argument - Cancel As Integer. To
cancel the form unloading you just need to set this argument to True at the
relevant point in your code. Looking at your code, you should just need to
replace the line...

DoCmd.CancelEvent

With...

Cancel = True

This should do the trick.

Good luck,


Stuart
 
Now I get a message box that gives me an run-time error '2501' The Close
action was cancelled which is the kind of message I'm trying to get rid of.

Thanks for your input
 
Hi Geoff,

Suddenly it all becomes clear!!! You're trying to close this form from
another procedure aren't you? The 2501 error is being caused by the procedure
trying to close the form, and not the form unload event. So you need to
handle this error in that procedure. I've popped a dummy procedure below with
the relevant error-handling for you to cut and paste. I've added a constant
to store the error number, as this makes the code a lot more easy to read...

__________________________________________________

Private Sub ErrorHandledCloseForm()
Const ERR_FORM_OPEN_CANCELLED As Long = 2501

On Error GoTo Error_Handler

DoCmd.Close acForm, "FormName"

Exit_Procedure:
Exit Sub

Error_Handler:
Select Case Err.Number
Case ERR_FORM_OPEN_CANCELLED
Resume Exit_Procedure
Case Else
MsgBox Err.Number & vbCrLf & Err.Description, vbExclamation,
"Unforeseen Error"
Resume Exit_Procedure
Resume
End Select
End Sub

__________________________________________________

Hope this helps!


Stuart
 
Doh! You might want to change the name of the constant in the dummy procedure
to:

ERR_FORM_CLOSE_CANCELLED

Sorry about that!

Stuart
 
Back
Top