There are occasions when the user may need to discard changes that they have made to a record that they are entering or modifying within a Microsoft Access database form. You may wish to allow them the option to undo these changes whilst editing the record and you will find below details to create this option.
You will see that the form below shows a record that has been entered into the database. The form displays the Undo button, however until the user begins to make any changes to this record the button is not enabled:
Form showing the undo button in the disabled state.
Once the user begins to modify the record the button is enabled to allow the user to undo the changes that they have made (the image below shows the phone number is being edited:
Form showing modifications being made to the record, in turn enabling the Undo Changes command.
The code behind this is very simple. The command button contains the following, that carries out the Undo action on the OnClick event:
Private Sub cmdUndoChanges_Click() DoCmd.RunCommand acCmdUndo End Sub
To ensure that the Undo option is only available whilst editing or adding a record then you will also need to ensure that you add the following event procedures to the Current event and Dirty event of the Form:
Private Sub Form_Current() Me!cmdUndoChanges.Enabled = False End Sub
Private Sub Form_Dirty(Cancel As Integer) Me!cmdUndoChanges.Enabled = True End Sub
Test it out on a form that you have created to see the results. More articles relating to Microsoft Access Forms can be found in the Microsoft Access Forms menu.