Access VBA code to close form and not save changes made

Anonymous
2025-04-30T09:14:27+00:00

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

Microsoft 365 and Office | Access | For business | Other

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.

0 comments No comments
{count} votes

5 answers

Sort by: Most helpful
  1. HansV 462.4K Reputation points MVP Volunteer Moderator
    2025-04-30T10:07:29+00:00

    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
    
    0 comments No comments
  2. Anonymous
    2025-04-30T11:07:47+00:00

    You might like to take a look at ChangedRecordDemo.zip in my Dropbox public databases folder at:

    https://www.dropbox.com/scl/fo/0scigd3r48hx5xrev2jrf/AB0-GMdTgMAO5O1cGdr3QW0?rlkey=ib6bs6g9jqcrywwzivur3265t&dl=0

    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.

    0 comments No comments
  3. ScottGem 68,775 Reputation points Volunteer Moderator
    2025-04-30T13:42:18+00:00

    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.

    0 comments No comments
  4. Anonymous
    2025-05-06T21:02:12+00:00

    Thank you for the guidance. It indeed worked !

    Warmest,

    Shane

    0 comments No comments
  5. Anonymous
    2025-05-06T21:12:43+00:00

    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

    0 comments No comments