Access 2007 Bound form: Close Question

jbakestull
jbakestull used Ask the Experts™
on
I've noticed that on a data entry form (bound) if a user makes a change to text field and doesn't save the record but exits out of the form, the previous value no longer remains.

What is the best practical way to prevent a user from making changes without prompting a warning message to the user?
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Top Expert 2016

Commented:
use the beforeupdate event of the form

private sub form_beforeupdate(cancel as integer)

if me.dirty then
   if (msgbox ("do you want to save changes made ?",vbyesno))=vbyes then
       me.dirty=false
       else
me.undo
       cancel=true
  end if
end if

end sub

Author

Commented:
I've never used me.dirty before.

Me. dirty = false (highlighted)

When I apply the code, rum time error message that displayed is '2115', the macro or function set to the beforeupdate or validation rule property for this feild is preventing Microsoft Office Access from saving the data in the feild.
DatabaseMX (Joe Anderson - Microsoft Access MVP)Database Architect / Application Developer
Top Expert 2007

Commented:
Well, you can't set Dirty = False in the Form BeforeUpdate event ... because Me.Dirty=False is what triggers the BU event ... And Dirty will already be True in the BU event.

I'm sure Capricorn1 meant a different event ...
Success in ‘20 With a Profitable Pricing Strategy

Do you wonder if your IT business is truly profitable or if you should raise your prices? Learn how to calculate your overhead burden using our free interactive tool and use it to determine the right price for your IT services. Start calculating Now!

Top Expert 2016

Commented:
sorry about that, try this revision

private sub form_beforeupdate(cancel as integer)

if me.dirty then
   if (msgbox ("do you want to save changes made ?",vbyesno))=vbyes then
       
       else
       me.undo
       cancel=true
  end if
end if

end sub
The problem I've have applying the code to form before update event is that it prompts a similar type of question over again even if the user has already saved the information before closing. To avoid confusion, I've placed an Me.undo before the close event. If the user forgets to press save, then information is lost. My reasoning why is that if the user notices that form will save on close even if save is not confirmed, I don't want user to become lazy. Also the save button creates has an additional fields like when record was last updated and by whom.    

Me.Undo
DoCmd.Close
DoCmd.OpenForm "FrmMain"

Author

Commented:
Apply dirty in before event wasn't the best logical choice.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial