How can I withdraw the form closing in beforeupdate events smoothly

  • Thread starter Thread starter Jason Jiang
  • Start date Start date
J

Jason Jiang

I add the following codes in BeforeUpdate event. When I click the Close
botton (X) in the right top of the form, it will let me choose if I wanna
save or discharge or cancel. When I clikc the cancel botton, it always shows
"You cannot save this records.....". How can I realize this fucntion
smoothly?

Private Sub Form_BeforeUpdate(Cancel As Integer)

If Me.Dirty = True Then

Select Case MsgBox("Click YES, Save and Close; Clikc NO, discharge
and Close; Click Cancel, Don't save and Don't close", vbYesNoCancel)

Case vbYes

Save = True

Case vbNo

Me.Undo

Case vbCancel

Cancel = -1

End Select

End If

End Sub
 
When I clikc the cancel botton, it always shows
"You cannot save this records.....". How can I realize this fucntion
smoothly?

Just put a Me.Undo line in the condition for Cancel.

John W. Vinson[MVP]
 
Sorry, it seems don't work.
Me.undo will withdraw all change in the form. In the condition of Cancel, I
don't want withdraw the change, just want to cancel the update event and
close event smoothly.

Can you help me?
 
Me.undo will withdraw all change in the form. In the condition of Cancel, I
don't want withdraw the change, just want to cancel the update event and
close event smoothly.

But if you're closing the form, and canceling the update event, you DO
want to withdraw the change. You're either saving the record as
modified, or you're not - and Cancel means (to me) that you are in
fact discarding the changes.

I guess I don't understand what you're trying to accomplish.

John W. Vinson[MVP]
 
Thank you very much.

Cancel means don't save, don't discarding the changes, don't close, don't do
anything and let the user keep editing.
for example: open the WORD and type some letters, then clikc the close
botton (X), it will ask us for three options. SAVE, No Save, and Cancel.

This cancel function is what I want.
 
Thank you very much.

Cancel means don't save, don't discarding the changes, don't close, don't do
anything and let the user keep editing.
for example: open the WORD and type some letters, then clikc the close
botton (X), it will ask us for three options. SAVE, No Save, and Cancel.

Ok, then:

Private Sub Form_BeforeUpdate(Cancel As Integer)
If Me.Dirty Then
Select Case MsgBox("Click YES, Save and Close; Click NO," _
& " discharge and Close; Click Cancel, " _
& " Don't save and Don't close", vbYesNoCancel)
Case vbYes
' Save = True
' there is no variable named Save. Just don't
' anything, or don't even bother testing for vbYes.
Case vbNo
Me.Undo
Case vbCancel
Cancel = True ' I'd use the constant not -1
End Select

End If

The variable named Save would have triggered an error if (as I would
strongly recommend!) you included Option Explicit in the module before
the first line of code.

You shouldn't need to do anything other than setting Cancel to True in
the form's BeforeUpdate. You may need additional similar code in the
form's Close event (which can also be cancelled).


John W. Vinson[MVP]
 
Back
Top