User Saving a Record else it deletes

C

Carter Wexler

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.
 
B

Brian

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).
 
C

Carter Wexler

Brian,

I tried the code yo gave me and it worked except it only does it on the
before update command which only runs when the user scrolls to another record
or tries to exit. When I placed the code under the save button it doesn't
update the record if the user hits yes, it will only undo the changes if the
user selects no, and the before update procedure will still run if the user
exits or scrolls. is there a way to call the before update procedure within
the save button? I tried placing the subs name to run with the save click
however i got an error.
 
B

Brian

The code I posted simply does not undo if the user clicks no, allowing it to
do its default action of saving when the user closes the form or navigates to
a different record. If you want to force a save action, use this in the code
for your button:

If Me.Dirty Then DoCmd.RunCommand acCmdSaveRecord

The form is "Dirty" if anything has been entered; this way, it won't try to
save if nothing has been entered and end up with a blank record. The save
action will, in turn, call Form_BeforeUpdate and Form_AfterUpdate.

You may want to have code in both places to allow the user to save manually
(you could leave out the prompt here, since the user's click of the Save
button would presumably be indication enough that he wants to save the
record) but also prompt the user to save when exiting the form or navigating
to a different record.
 

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

Top