Link to home
Start Free TrialLog in
Avatar of Frank Freese
Frank FreeseFlag for United States of America

asked on

close a form with or without saving changes

I would like to be able to advise the user when they close a form and there has been changes on if they want to save the changes or not
Avatar of avbsql
avbsql
Flag of United States of America image

Ru looking to let the user know or have a pop up message to give an option to save or not save the changes...is that the question?
Avatar of Frank Freese

ASKER

When a form is closed a pop up message would appear asking them if they want to save the record. If the say no then no record is written
Avatar of Rey Obrero (Capricorn1)
is this a bound form?
Private Sub Form_UnLoad(Cancel As Integer)

If Me.Dirty = True Then
       If MsgBox ("Do you want to save the changes?", 20,Me.Caption) = vbYes Then
           Me.Dirty = False
       End If
End If
End Sub
Mod:

Private Sub Form_UnLoad(Cancel As Integer)

If Me.Dirty = True Then
       If MsgBox ("Do you want to save the changes?", 20,Me.Caption) = vbYes Then
           Me.Dirty = False
       Else
           Me.Undo                 ' **** ADD THIS
       End If
End If
End Sub
The form is bound. Some of the fields have default values and those controls are locked. The me.dirty=true is false because the user did not actually enter any data themselves.
If not data was entered, what is there to save ?

mx
the defaults arew being saved
Defaults are not Saved unless the record is made Dirty by and Edit of some bound field.

mx
i stand corrected. however, when I did put data in the form_unload me.dirty came back as false not true and the msgbox was not executed.
fh_freese,
how are your users close the form? do you have command button for this?
yes.

Docmd.close
Docmd.openform "Main Menu"
ASKER CERTIFIED SOLUTION
Avatar of DatabaseMX (Joe Anderson - Former Microsoft Access MVP)
DatabaseMX (Joe Anderson - Former Microsoft Access MVP)
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial


Private Sub btnClose()

If Me.Dirty = True Then
       If MsgBox ("Do you want to save the changes?", 20,Me.Caption) = vbYes Then
           Me.Dirty = False
       Else
           Me.Undo                 ' **** ADD THIS
       End If
End If
docmd.Close acForm, me.name
End Sub

Also ... turn off the Close X button on the form property sheet.
thanks. hope not to revisit this. much appreciated.