none
Private Sub If Me.Dirty ... will data be saved on close RRS feed

  • Question

  • Hi

    I try to understand "If Me.Dirty Then Me.Dirty = False"

    If I have the code below on a MainForm  click button. Will data on MainForm and SubForm be saved before the Form is closed?

    Private Sub ctlSaveAndClose_Click()
      
      If Me.Dirty Then Me.Dirty = False
      DoCmd.Close acForm, Me.Name
      
    End Sub


    Best // Peter Forss Stockholm and Sigtuna GMT +1.00

    Monday, August 10, 2015 2:33 PM

Answers

  • In the AfterUpdate event of a control, you know that the record has been modified - otherwise the AfterUpdate event wouldn't have occurred. So there is no need to test If Me.Dirty Then. You can simply use

    Me.Dirty = False


    Regards, Hans Vogelaar (http://www.eileenslounge.com)

    • Marked as answer by ForssPeterNova Wednesday, August 12, 2015 11:21 AM
    Monday, August 10, 2015 6:02 PM
  • Yes.

    When the focus is on the main form (for example when you click a button on the main form), the active record in all subforms have already been saved.

    If the active record in the main form has been changed since it was last saved, Me.Dirty will be True. Setting Me.Dirty to False will save the record.

    If the active record in the main form has not been changed, Me.Dirty is False; in that case there is no need to save the record, of course.


    Regards, Hans Vogelaar (http://www.eileenslounge.com)

    • Marked as answer by ForssPeterNova Wednesday, August 12, 2015 11:21 AM
    Monday, August 10, 2015 2:54 PM

All replies

  • Closing your form will automatically cause the record to be saved.  So your check for dirty and then dirty = false is not entirely necessary.  But, it isn't hurting anything.

    As for your sub form, once you leave it and return focus to the main form, the sub form's record is automatically saved.  I assume that your command button is on the main form.  When you click it, you have left the sub form and are now back on the main form.

    • Edited by RunningManHD Monday, August 10, 2015 2:52 PM
    • Proposed as answer by ryguy72 Wednesday, August 12, 2015 9:12 PM
    Monday, August 10, 2015 2:50 PM
  • Yes.

    When the focus is on the main form (for example when you click a button on the main form), the active record in all subforms have already been saved.

    If the active record in the main form has been changed since it was last saved, Me.Dirty will be True. Setting Me.Dirty to False will save the record.

    If the active record in the main form has not been changed, Me.Dirty is False; in that case there is no need to save the record, of course.


    Regards, Hans Vogelaar (http://www.eileenslounge.com)

    • Marked as answer by ForssPeterNova Wednesday, August 12, 2015 11:21 AM
    Monday, August 10, 2015 2:54 PM
  • Today I red Allen Browne

    "When you enter data into a form, Access saves the record automatically. An error message notifies you if the record cannot be saved. Or does it?

    Use the Close action or method, and the form fails to notify you if the record cannot be saved. The form closes, and your entry is silently lost.

    To demonstrate this flaw, download AccessFlaws.zip, open the form LosingData, and follow the instructions.

    (Note: if you close the form with the [X] at the right end of the form's title bar, you do receive a warning that the record cannot be saved. Typically the problem occurs when you place a Close command button on your form.)"

    Allen Browne page

    I have users swearing that they have edited or registered new data and next time they open the form, data is lost. The data is (was) on the SubForm.

    So is it necessary or not to use code to force Access to save data?


    Best // Peter Forss Stockholm and Sigtuna GMT +1.00

    Monday, August 10, 2015 5:38 PM
  • One more question on Me.Dirty...

    What if I use If Me.Dirty then Me.Dirty = False on a specific field on a Form. 

    EventProcedure AfterUpdate:

    If Me.Dirty then Me.Dirty = False




    Best // Peter Forss Stockholm and Sigtuna GMT +1.00

    Monday, August 10, 2015 5:58 PM
  • In the AfterUpdate event of a control, you know that the record has been modified - otherwise the AfterUpdate event wouldn't have occurred. So there is no need to test If Me.Dirty Then. You can simply use

    Me.Dirty = False


    Regards, Hans Vogelaar (http://www.eileenslounge.com)

    • Marked as answer by ForssPeterNova Wednesday, August 12, 2015 11:21 AM
    Monday, August 10, 2015 6:02 PM
  • Thanks Hans

    But is it recommended to use it on a control?


    Best // Peter Forss Stockholm and Sigtuna GMT +1.00

    Monday, August 10, 2015 6:08 PM
  • If you want to make sure that the record is saved each time the user changes the value of a specific field, use the After Update event of the control bound to that field.

    Otherwise, it is not strictly necessary to save the record explicitly. Access saves records automatically.


    Regards, Hans Vogelaar (http://www.eileenslounge.com)

    • Proposed as answer by ryguy72 Wednesday, August 12, 2015 9:13 PM
    Monday, August 10, 2015 6:21 PM