Cancelling the close form statement.



I have an EditRecord form that detects if changes have been made to the
record on the form. I have two buttons; cmdsave and cmdclose. When I click
cmdsave everything is saved perfectly. When I click cmdclose I am prompted,
if changes have been made, to save the changes. Code on cmdClose is:
DoCmd.Close acForm, "Frm_EditBooking", acSaveNo
On the form unload event I have:
If fDataChanged Then

Select Case MsgBox("Do you want to save changes?", vbYesNoCancel)
Case vbYes '*****Problem occurs when this criteria is true****
Exit Sub
Case vbNo
'do nothing
Case vbCancel
Cancel = True
Exit Sub
End Select
End If

cmdSave_click gets to the line:
DoCmd.Close acForm, "Frm_EditBooking", acSaveYes
Then detects an error displaying "The close action was cancelled" Is this
because the form is attempting to close twice? I can't work it out! Can
someone please help!!



Jeff Boyce


It appears you are waiting until the form unload event to test for changes
and save. What happens if you move this process/code up earlier, say, in
the form's Close event?


Jeff Boyce
Microsoft Office/Access MVP


First, the close event can't be canceled
Next, the Unload event happens prior to the close. Here is the order of

Unload → Deactivate → Close

The question is what happens in cmdSave_Click?
Also, is this a bound or unbound form?
If it is a bound form, nothing would be required if Save were selected.
Closing the form updates the record. If you don't want to safe the record,
the Me.Undo would cause the record not to be saved.


Jeff, Knowing the order of events changes everything. My code is written
assuming close occurs before unload. A few changes required!! Thanks for your


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

Similar Threads

Cancel timer event if criteria not met 1
vbOkCancel 4
vbYesNoCancel 14
Close Open Reports with Code 1
NotInList Firing Problem 12
Problem with closing a form 4
Cancel a Save 1
Record Change Confirmation 8