Link to home
Start Free TrialLog in
Avatar of SteveL13
SteveL13Flag for United States of America

asked on

Behavoir for two command buttons on a form

I have a form with two command buttons.  One creates a new record and the other one closes the form.  But if the user has changed data in an existing record that appears in the form I want the user to receive a warning that the change they made will be saved if they answer 'Yes' when either button is clicked.  If they answer 'No' then the change should not be made to the record.

And then the New Record button will go ahead and open the form for new data and NOT make the change.
If the Close Form button is clicked then the form will close and the change will not be made.


What would this If MsgBox code look like?
Avatar of Dale Fye
Dale Fye
Flag of United States of America image

This should resolve the issue with the Save button.  If the form is dirty, it will display a message and if you select yes, it will save the record, if you select no, it will cancel any changes you have made.  Then it will close the form.
Private Sub cmd_Close

    if me.dirty Then

        if msgbox("You have unsaved data!" & vbcrlf & "Save?", vbcritical + vbYesNo) = vbYes Then
            me.dirty = false
        else
            me.undo
        end if

        docmd.close acform, me.name

End Sub

Open in new window

Not quite sure what you want the New Record button to do.  If the form is dirty, do you want it to ask about saving (as above) before moving to the new record?
Avatar of SteveL13

ASKER

"If the form is dirty, do you want it to ask about saving (as above) before moving to the new record? "  =  Yes.
This question needs an answer!
Become an EE member today
7 DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform.
View membership options
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.