verify changes

  • Thread starter Thread starter Walter
  • Start date Start date
W

Walter

I want to verify any changes made to a form after the
original entry. I know this can be done on the form's
BeforeUpdate event. I have figured out the MsgBox's
prompt, buttons, default button, and title but don't
understand from there. What I am trying to do is have a
command button to save and close the form. I have this
working(I added the DoCmd.Close Form statement to the code
created by the wizard for the save command). When data
has changed and BeforeUpdate comes into play, I would like
3 choices;yes, no, cancel. Yes=save and close, no=don't
save and close, cancel=close MsgBox and remain open. I
think this has to be done with an If Then statement but I
don't know how. If someone could please tell me and
explain in laymen's terms what each thing does, I would
appreciate it and maybe it will help me learn how to use
other things.
Thanks in advance,
Walter
 
Walter, this is a little awkward, since Access processes the events
differently when a form is being closed. The best way around this is to
explicitly save the record *before* closing the form, so it is easy NOT to
close if you want to pull out.

Private Sub cmdClose_Click()
On Error Goto Err_Handler
If Me.Dirty Then
Me.Dirty = False
End If
DoCmd.Close acForm, Me.Name
Exit_Handler:
Exit Sub

Err_Handler:
Select Case Err.Number
Case 3314, 2101, 2115
MsgBox "Record cannot be saved at this time." & vbCrLf & _
"Complete the entry, or press <Esc> to undo."
Case Else
MsgBox "Error " & Err.Number & " - " & Err.Description
End Select
Resume Exit_Handler
End Sub

Setting Dirty to False forces Form_BeforeUpdate to be called. If the save
fails, the Close line never executes.

Private Sub Form_BeforeUpdate(Cancel As Integer)
Select Case MsgBox("Save?", vbYesNoCancel)
Case vbYes
'do nothing. Default behavior.
Case vbNo
'Cancel the update, and undo the form so it can close.
Cancel = True
Me.Undo
Case vbCancel
'Cancel the update, and leave it editing.
Cancel = True
End Select
End Sub
 
Back
Top