• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 699
  • Last Modified:

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.
0
PipMic
Asked:
PipMic
  • 6
  • 5
  • 4
  • +1
2 Solutions
 
MarkVrenkenCommented:
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
0
 
masteripperCommented:
There must be some referential integrity that prevents u from simply closing the form
0
 
Dale FyeCommented:
Or, you could actually perform the save for them.

Private sub cmd_Close_Click

    dim strMsg as string

    if me.dirty Then

        strMsg = "Would you like to save the data before exiting?"
        if msgbox(strMsg, vbcritical + vbYesno, "Unsaved data") = vbNo Then
            me.undo              'removes any changes
        else
            me.dirty = false   'saves the data
        endif

    End If

    docmd.close acform, me.name

End Sub
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
MarkVrenkenCommented:
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

0
 
MarkVrenkenCommented:
Sorry i presumed it was a unbound form. If it is a BOUND form you should try fyeds suggestion
0
 
PipMicAuthor Commented:
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
0
 
Dale FyeCommented:
Access doesn't particularly like running the & and other items together.  Try:

& vbcrlf &
0
 
MarkVrenkenCommented:
yes i made a mistake, sorry. this will work if it's an unbound form like i said earlier.

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

0
 
PipMicAuthor Commented:
Hi fyad,

Your code works...

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

Thanks
0
 
PipMicAuthor Commented:
Hi Mark your updated code works too..

Thanks
0
 
PipMicAuthor Commented:
Two excellent pieces of code....
0
 
Dale FyeCommented:
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.
0
 
PipMicAuthor Commented:
Thanks fyed...grateful for the explanation  :)
0
 
Dale FyeCommented:
glad to help
0
 
PipMicAuthor Commented:
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?
0
 
Dale FyeCommented:
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.
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 6
  • 5
  • 4
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now