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
Frank FreeseAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

avbsqlCommented:
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?
0
Frank FreeseAuthor Commented:
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
0
Rey Obrero (Capricorn1)Commented:
is this a bound form?
0
The Ultimate Tool Kit for Technolgy Solution Provi

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy for valuable how-to assets including sample agreements, checklists, flowcharts, and more!

DatabaseMX (Joe Anderson - Microsoft Access MVP)Database Architect / Systems AnalystCommented:
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
0
DatabaseMX (Joe Anderson - Microsoft Access MVP)Database Architect / Systems AnalystCommented:
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
0
Frank FreeseAuthor Commented:
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.
0
DatabaseMX (Joe Anderson - Microsoft Access MVP)Database Architect / Systems AnalystCommented:
If not data was entered, what is there to save ?

mx
0
Frank FreeseAuthor Commented:
the defaults arew being saved
0
DatabaseMX (Joe Anderson - Microsoft Access MVP)Database Architect / Systems AnalystCommented:
Defaults are not Saved unless the record is made Dirty by and Edit of some bound field.

mx
0
Frank FreeseAuthor Commented:
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.
0
Rey Obrero (Capricorn1)Commented:
fh_freese,
how are your users close the form? do you have command button for this?
0
Frank FreeseAuthor Commented:
yes.

Docmd.close
Docmd.openform "Main Menu"
0
DatabaseMX (Joe Anderson - Microsoft Access MVP)Database Architect / Systems AnalystCommented:
"however, when I did put data in the form_unload me.dirty came back as false not true and the msgbox was not executed."

Then the record is already being save via some other way, like going to a new record or clicking into a subform (if there is one) ... or click Shift+Enter.

In fact, It's probably being saved as the result of clicking the X button on the form ... I just came across this recently.  You may need a Close button ... and the put that code in the Close button Click event.

mx
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
DatabaseMX (Joe Anderson - Microsoft Access MVP)Database Architect / Systems AnalystCommented:


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.
0
Frank FreeseAuthor Commented:
thanks. hope not to revisit this. much appreciated.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft Access

From novice to tech pro — start learning today.