Return to Snippet

Revision: 45464
at May 1, 2011 03:01 by heislekw


Updated Code
If Me.Dirty Then
   RunCommand acCmdSaveRecord
End If

DoCmd.Close acForm, Me.Name

Options: 

To force a record to be saved, you could use:

#1.  RunCommand acCmdSaveRecord
#2.  DoCmd.DoMenuItem acFormBar, acRecordsMenu, acSaveRecord, , acMenuVer70
#3.  Me.Refresh
#4.  Me.Dirty = False

#1 fails if the form does not have focus.
#2 (generated by wizard in Access 95 to 2003) has the same problem, and also fails with pop-up forms in some versions.
#3 works, but older versions of Access do not generate a message if the save fails.
#4 specifies which form, and gives an error message if the save fails. The message may not be clear (e.g. "The property could not be set"), and the approach does not work in Access 1 or 2, but it is the most reliable approach unless you are working with the ancient versions.

Revision: 45463
at May 1, 2011 02:47 by heislekw


Initial Code
If Me.Dirty Then
        RunCommand acCmdSaveRecord
    End If
    DoCmd.Close

Initial URL
http://allenbrowne.com/ser-31.html

Initial Description
Performing a Close action on a form can cause you to lose edits without warning.

The problem occurs if there is any reason why the record cannot be saved. e.g.: 
    * a required field was left blank;
    * the record would create a duplicate in a unique index;
    * the form's Before Update event was canceled;
    * a Validation Rule was not met.

Access simply discards your edits and closes the form without warning that the record was not saved. 

Develop the habit of explicitly saving whenever you do anything that requires the record to be saved, e.g. applying or removing a Filter or OrderBy property, changing a RecordSource, or moving to another record.

You must explicitly save the record before executing a Close action. For example:

Initial Title
Close Without Record Save

Initial Tags

                                

Initial Language
Visual Basic