The acSaveNo/acSaveYes/acSavePrompt only applies to the design of the form, not to the current record.
Add the following line above DoCmd.Close:
If Me.Dirty Then Me.Undo
This browser is no longer supported.
Upgrade to Microsoft Edge to take advantage of the latest features, security updates, and technical support.
Hello,
I have created a form in Access that has a command button for the user to select when they want to exit the form without committing the changes made. The code attached to the button's click event is:
Private Sub btnExitDontSave_Click()
DoCmd.Close acDefault, acSaveNo
DoCmd.OpenForm "MainSwitchBoard"
End Sub
The form closes and returns to the main switchboard, but it retains the changes made to the record.
Anyone have a suggestion on how to modify the code so that it doesn't save the changed data?
Many thanks
Locked Question. This question was migrated from the Microsoft Support Community. You can vote on whether it's helpful, but you can't add comments or replies or follow the question.
The acSaveNo/acSaveYes/acSavePrompt only applies to the design of the form, not to the current record.
Add the following line above DoCmd.Close:
If Me.Dirty Then Me.Undo
You might like to take a look at ChangedRecordDemo.zip in my Dropbox public databases folder at:
In this little demo file the Form to Confirm Changes to Data option illustrates how to prompt the user to save or abort changes to the current record. Note that only actual changes to the values are detected , rather than merely updates. A record can be updated without the values of data having changed since the form was moved to the current record.
The code in the form's module firstly writes the current values to an array when the form is moved to a record. It then writes the values to another array and compares the two arrays. If any data have changed the user is prompted to confirm whether or not to save the changes. If not, the record is undone.
Another approach is illustrated in SaveDemo.zip in the same Dropbox folder. In this the user is forced to save, undo, or close the form by means of command buttons, which are enabled/disabled by code in the form's module on the basis of the current state of the record. In this case it detects all updates rather than those which have made actual changes to values.
As Hans said, the Save prompt refers to changes in the Design of the form NOT to data.
With Access, as soon as a record loses focus the changes to the record are committed. In my opinion, if there is a need to allow the user to not commit the changes, using an unbound form might be a better choice.
With an unbound form, you would have to write the code that retrieves the existing record and then code that saves the record to the table. If the code is not run, then the record isn't saved.
Thank you for the guidance. It indeed worked !
Warmest,
Shane
You have quite the treasure trove on your Drop Box. Thank you for sharing it and no doubt I'll discover lots of things to learn.
Much appreciated.
Shane