Link to home
Start Free TrialLog in
Avatar of PipMic
PipMicFlag for Gibraltar

asked on

Close or Exit Button with a message

Hi all,

I have created a Close button on a form... However I would like the button to close the form and not save any data that has so far being entered in the form... eg the Msg box that should appear on pressing the Close button should be something like:-

"Please press the Save button to save the data. This Close button will exit the form and the data will not be saved"

Gateful for ideas.
Avatar of MarkVrenken
MarkVrenken
Flag of Netherlands image

Lresponse = MsgBox("Do you really want to close without saving?" &vbcrlf& "Please press the Save button on the form to save the data. This Close button will exit the form and the data will not be saved.", vbYesNo, "Warning")

    If Lresponse = vbNo Then
        Exit Sub
   
    Else
    docmd.close
    End If

something like this
Avatar of masteripper
masteripper

There must be some referential integrity that prevents u from simply closing the form
SOLUTION
Avatar of Dale Fye
Dale Fye
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
sorry minor bugs hadn't tested it yet
Dim lresponse As Integer

lresponse = MsgBox("Do you really want to close without saving?" & vbCrLf & "Please press the Save button on the form to save the data. This Close button will exit the form and the data will not be saved.", vbYesNo, "Warning")

    If lresponse = vbNo Then
        Exit Sub
    
    Else
    DoCmd.Close
    End If

Open in new window

Sorry i presumed it was a unbound form. If it is a BOUND form you should try fyeds suggestion
Avatar of PipMic

ASKER

my code now looks something like this

Private Sub close_form_Click()


Lresponse = MsgBox("Do you really want to close without saving?" &vbcrlf& "Please press the Save button on the form to save the data. This Close button will exit the form and the data will not be saved.", vbYesNo, "Warning")



    If lresponse = vbNo Then
        Exit Sub
   
    Else
    DoCmd.close
   
    End If
End Sub

The entire Msgbox line is in red and the event fails in that line
Access doesn't particularly like running the & and other items together.  Try:

& vbcrlf &
ASKER CERTIFIED SOLUTION
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
Avatar of PipMic

ASKER

Hi fyad,

Your code works...

can you briefly explain the code and each line does, eg the dirty bit!!!

Thanks
Avatar of PipMic

ASKER

Hi Mark your updated code works too..

Thanks
Avatar of PipMic

ASKER

Two excellent pieces of code....
The Dirty property of the form can be used to determine whether any bound fields have been changed since the current record was accessed.  If you change any value (must be bound field), then the Dirty property will be set to true.

However, you can also use the Dirty property to force the record to be saved by setting:

Dirty = False

This does the same thing as moving off the record, or the MenuItem or RunCommand that saves the record.
Avatar of PipMic

ASKER

Thanks fyed...grateful for the explanation  :)
glad to help
Avatar of PipMic

ASKER

Could i bother you with one final question?

Both sets of code work well,  but both of them are different. When should I use one method over the other?
They really aren't that different.

My code provides you with a way to actually prompt the user and accept that response to actually do the save, while at the same time providing them a way to leave without saving.

I do it that way, so that they don't have to click another button after acknowledging the message.

But if I have error checking or data validation in the cmd_Save click event, use the first method, which says "click the Save button", or I'll display the message and if they indicate they would like to save the record, I will call the cmd_Save_Click subroutine rather than use the me.Dirty = False technique.

As you learn more about programming you will find that there is almost always more than one way to accomplish what you want to do.  Some are more elegant than others, and some (even some unelegant methods) are faster than others.  I like "elegant" code, but will use brute force if it is faster, for my clients sake.