User Saving a Record else it deletes

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

I want a user editing a record to be able to save the record or else cancel.
If the user cancels I want to have the record return to it's original value,
before the edit.

How do I go about preserving the original record before the edit in case a
user does a cancel or closes with the intent of not saving the data. I know
access autmatically saves data edited ina form after it's been put in.
 
The original data is retained until the form updates, which can be forced by
forcing a record save, or is automatic when closing the form or moving to a
new record.

Make a Cancel button (let's call it ButtonCancel) on the form, and make its
code something like this:

Private ButtonCancel_Click()
If Not Me.Dirty then Exit Sub 'simply exit if nothing has been changed
Me.Undo 'undo changes
End Sub

This at least gives the user an option to cancel input and have the values
restored to those before the edit. Preventing a form closure or move to a new
record in error is more difficult; how do you know whether the user wanted to
save changes or not? You could do this:

1. Turn off the navigation buttons so that the user cannot simply go to a
new record. If you do it this way, you will need to create a button to make a
new record, and be sure to ask the user if he wants to save the current
record when he clicks ButtonNew also, using the code below.
2. Create a Close button and add a confirmation dialog there:

Private Sub ButtonClose_Click()
If Me.Dirty Then 'only if record has been changed
If MsgBox("Do you want to save your changes?", vbExclamation + vbYesNo,
"Save record?") = vbNo Then Me.Undo
DoCmd.Close
End Sub

If something has been changed, this asks the user if he wants to save his
changes. If so, it closes (thus forcing the record to be saved); if not, it
undoes the changes before closing.

You might also put a confirmation dialog in Form_BeforeUpdate and do a
Cancel & Undo if the user answers "No." This way, it will always ask, even if
the user finds another way to move to a new record (such as rolling the mouse
wheel after changing something).
 
Back
Top