Prompt before closing form

fluidmind

Registered User.
Local time
Today, 05:06
Joined
Jun 30, 2006
Messages
66
Hi guys!

When closing a form, I want my system to prompt: "Close without saving?". If yes, I want to continue the closing-proces, but when "no" is pressed, I want to just close the MsgBox-window and of course keep all the data-entries...

Code:
If MsgBox("Close without saving?", vbYesNo, "Close?") = vbNo Then XXXXXXX

What do I write instead of the X'es???

// JR
 
I think all you need to do change your vbNo to vbYes and put the close form command in the if statement... :)
 
nope

It may sound weird. But even with all the help from other similar questions, I can't get it to work :-( Would it be possible for any of you to maybe post the entire code??? On yes, the msgbox and the window behind it closes. On no, the msgbox closes but the window behind it stays open...

I would really appreciate your help on this one. My boss has requested a solution in a while now, and the users are apparantly pressing the close-button all the time and losing data :-(. What is wrong with people :-)

Thanks in advance
 
See below

Try these 2 pieces of code - 1 in the Form_BeforeUpdate and command button on Click. I have resourced them from others. All credit to them.

Private Sub Form_BeforeUpdate(Cancel As Integer)

'Check if there are any changes in the data
If Me.Dirty Then
If MsgBox("Do you want to save the changes?", vbYesNo) = vbNo Then
'Undo all the changes
Me.Undo
End If
End If
End Sub

Private Sub cmdUpdate_Click()
On Error GoTo Err_cmdUpdate_Click
DoCmd.DoMenuItem acFormBar, acRecordsMenu, acSaveRecord, , acMenuVer70
DoCmd.Close

Exit_cmdUpdate_Click:
Exit Sub

Err_cmdUpdate_Click:
MsgBox Err.Description
Resume Exit_cmdUpdate_Click

End Sub
 
Easier to take the close button off of the form which will force users to use a command button to close.
 
jkl0 said:
Easier to take the close button off of the form which will force users to use a command button to close.
Easier maybe, safer, not so
 
Hi guys!

Thanks so much for your help. I'm getting a little desperate here... I really don't understand why this isn't working. I've tried all you guys' suggestions, I've checked out the entire forum for answers and I've googled my ass off. But this little simple thing still doesn't work. Here is my entire code:

Code:
Private Sub Form_Close()

If MsgBox("Do you really want to close this window?", vbYesNo, "Title") = vbNo Then Cancel = True

End Sub

You see. It's not a matter of saving or not. It is about closing the window or not. If no is pressed, I want to cancel the proces started when the user pressed the red x. Why is this apparently so difficult???
 
You have to use the forms BeforeUpdate event if you want to be able to CANCEL an event. Did you check out the link I gave you in my first response above? It contains some more links that will show you how to do it.
 
So did the solution for this ever appear? I have exactly the same thing I want to do and found this via Google.
 
So did the solution for this ever appear? I have exactly the same thing I want to do and found this via Google.
As GHudson stated - you have to use the form's BEFORE UPDATE event to cancel an update.

I would post a separate thread to deal with your problem.
 
Well, the problem's the same, so it's logical to keep it on the same thread. The problem with BeforeUpdate is that the same message box "Are you sure you want to whatever" that has been created will pop up every time you skip to another record (which may happen a lot in the case of continuous forms). So the question remains: is there a way to hit 'cancel' on a messagebox after you have hit the 'x' to close the form?
 
Well, the problem's the same, so it's logical to keep it on the same thread.
Well, you can do that. However, I would suggest to you that you will get much more help if you don't. Because many people who help out on the board do not visit posts with 20-30 posts in it because they either think someone else is already helping or they only look for posts with 0 to 3 posts in them. The longer threads are typically a major conversation, which can also turn off immediately many qualified people trying to help.

And, I had a moderator actually tell me one time that it is best to post a new thread especially if the existing thread was over a few months old.

So your choice...

Now as far as cancelling -

"Are you sure you want to whatever" that has been created will pop up every time you skip to another record (which may happen a lot in the case of continuous forms).

No, that is not correct. The BEFORE UPDATE event ONLY fires if something has CHANGED on that record. Just moving between them would not fire it (unless you had something changing on the record).
 
Yes, exactly, popup appears when skip to another record after a change has been made to the previous record.

Thanks.
 
Yes, exactly, popup appears when skip to another record after a change has been made to the previous record.

Thanks.
So, What's the problem? I don't understand. The Before Update event would be where you validate what is entered:

Code:
If Len(Me.TextBox & "") = 0 Then 
   Cancel = True
   If MsgBox("Textbox needs info, do you want to cancel this record?, vbQuestion + vbYesNo, "Cancel Record?") = vbYes Then
       Me.Undo
End If
[code]
 
The message box would only appear if the field was left null or had an empty string.  Otherwise it would quietly just go to the next record.  If the validation failed it asks if they want to cancel the record.  If yes is chosen then it will undo the record.  If they say no, it just goes on as if nothing happened.
 
As I mentioned before, onUpdate does not suit my needs (yes, just the way you've presented the code). I need this to work onClose. Any other takers?
Thanks,
 
As I mentioned before, onUpdate does not suit my needs (yes, just the way you've presented the code). I need this to work onClose. Any other takers?
Thanks,
Okay, I don't think you understand when events fire. If you close the form and something has changed on the form - the Before Update event will fire. If you move to another record, and something has changed, the Before Update will fire. Before update is where you do this; PERIOD, end of story. What part of this is vague and what does the Close event have to do with this???
 
I do understand how the events work, that is how I know at onUpdate isn't good enough, thank you.
 
I do understand how the events work, that is how I know at onUpdate isn't good enough, thank you.
Your answer isn't good enough either. Explain WHY it won't work?
 

Users who are viewing this thread

Back
Top Bottom