Full Version: Save and Discard Changes in a Form
UtterAccess Forums > Microsoft® Access > Access Forms
Hi All,
I have a form which is used to make changes to and isert new records in a linked table. Currently whatever changes I make to any control in the form are reflected in the table immediately. Can I have a Save or a Discard button which will let me either accept or neglect whatever I have done in the form so far. i.e. currently if I change say 5-6 values and then realize I didn't want to make a change, I have to go back and retype the old values... I want to be able to just exit the form without saving the changes...
Also, I have a feature which sets the date value to sysdate whenever a field is modified. The way I am doing it is through the following code:
Private Sub Form_Dirty(Cancel As Integer)
Me.date = Now()
End Sub
The problem with this if the user changes a value X to Y and then back to X, it still updates the date. Can I check for an ACTUAL change in the value rather than any keystroke?
I will appreciate any suggestions...
If you are using a bound form, then the values will be updated when you make changes. You can have a button that will cancel (DoCmd.Undo) the changes.
For the second part you would have to capture the original value. You might want to consider looking up Unbound forms here at UA for instructions on how to do the I/O yourself.
One last point, Date is a reserved word in Access and shouldn't be used for object names.
1) If all of your controls are bound and you don't have any subforms, a simple UNDO button with the following code will undo all changes made to a record (assuming you haven't moved on to a different record already):


2) Rather than using the DIRTY event, you could use the BEFOREUPDATE event. This will attempt to set the date when the record is being committed to the DB, rather than immediately when the user types anything in. HOWEVER, if a user physically changes an entry, then physically changes it back (rather than just using UNDO), it will still fire and change the date. The only way to avoid this would be, in your beforeupdate event, to check the current values in the controls one by one against the original values, and only change the date if something was different.
this is how access handles bound forms. As soon as the record loses focus it will be stored in the underlying source. If you don't want that then use either unbound forms and append the data programmatically as you wish and when, or use the undo command to undo whatever was saved in the table.
Some sample code could look like:

If Me.Dirty = True Then

Select Case MsgBox("Want to save...!", vbYesNo Or vbExclamation Or vbDefaultButton1, Application.Name)
Case vbYes
Case vbNo
End Select

Good luck
Hi Everyone,
Thanks a lot for the responses. I think I will have to go the Unbound form route...
Oused date just as an example (should have used some other name anyways), it's not the actual control name. I do have an undo button but as pointed out it only reverses the current record change and I want to reverse all changes...
Thanks for all your suggestions. I'll post again once I try the new methods suggested above.
Thought I should give you some sample code that may work to fix your updating of the date problem. This steps through all the controls on the form, comparing their current value to their "oldvalue", which is a property that exists and is populated ONLY with bound controls. If it finds any changes from the original values, it will update the date. BTW, as Scott noted, you should not be using "date" as a field name.
Private Sub Form_BeforeUpdate(Cancel As Integer)
  Dim blnChanged As Boolean
  Dim ctl As Control
  On Error Resume Next
  For Each ctl In Me.Controls
    blnChanged = (ctl.Value <> ctl.OldValue)
    If blnChanged Then Exit For
  Next ctl
  On Error GoTo 0
  If blnChanged Then Me.date = Now()
End Sub
This is a "lo-fi" version of UA. To view the full version with more information, formatting and images, please click here.