Posted By

heislekw on 05/01/11


Tagged

save close


Versions (?)

Close Without Record Save


 / Published in: Visual Basic
 

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

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:

  1. If Me.Dirty Then
  2. RunCommand acCmdSaveRecord
  3. End If
  4.  
  5. DoCmd.Close acForm, Me.Name
  6.  
  7. Options:
  8.  
  9. To force a record to be saved, you could use:
  10.  
  11. #1. RunCommand acCmdSaveRecord
  12. #2. DoCmd.DoMenuItem acFormBar, acRecordsMenu, acSaveRecord, , acMenuVer70
  13. #3. Me.Refresh
  14. #4. Me.Dirty = False
  15.  
  16. #1 fails if the form does not have focus.
  17. #2 (generated by wizard in Access 95 to 2003) has the same problem, and also fails with pop-up forms in some versions.
  18. #3 works, but older versions of Access do not generate a message if the save fails.
  19. #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.

Report this snippet  

You need to login to post a comment.