PDA

View Full Version : Solved: Message Box closing issues



FF Ethan
05-20-2010, 02:37 PM
I have a MsgBox that will close even though I click "No" can someone help?
On Error Resume Next
YesNo = MsgBox("Are you sure you want to close?", vbYesNo + vbCritical, "This will close the form.")
If No Then
DoCmd.Close_Click = False
End If

DoCmd.Close
Exit_Close_Click:
Exit Sub
Err_Close_Click:
MsgBox Err.Description
Resume Exit_Close_Click

End Sub

austenr
05-20-2010, 09:00 PM
Hard to tell from the code you posted. Can you post the whole sub or at least the part before On Error Resume Next

Might try to change your msgbox and if statement from this:

YesNo = MsgBox("Are you sure you want to close?", vbYesNo + vbCritical, "This will close the form.")
If No Then
DoCmd.Close_Click = False
End If


To this:

MsgBox ("Are you sure you want to close? " & "This will close the form."), vbYesNo + vbCritical
If vbNo Then
DoCmd.Click_Close = False
End If

OBP
05-21-2010, 08:36 AM
I use

MsgBox ("Are you sure you want to close? " & "This will close the form."), vbYesNo + vbCritical
If YesNo = vbNo Then Exit Sub

FF Ethan
05-21-2010, 08:45 AM
Private Sub Close_Click()
On Error Resume Next
YesNo = MsgBox("Are you sure you want to close?", vbYesNo + vbCritical, "This will close the form.")
If vbNo Then
DoCmd.Close_Click = False
End If






DoCmd.Close
Exit_Close_Click:
Exit Sub
Err_Close_Click:
MsgBox Err.Description
Resume Exit_Close_Click

End Sub

Sorry I thought i had all of it if you need more let me know.

FF Ethan
05-21-2010, 08:47 AM
OBP I tried yours but it says End if Without Block If?
I dont know what I am doing wrong.

FF Ethan
05-21-2010, 08:48 AM
austenr your code says Method or data Member not found?

FF Ethan
05-21-2010, 08:49 AM
Option Compare Database

Private Sub New_Click()
On Error Resume Next
OkOnly = MsgBox("Please enter a value first!", vbOKOnly = vbCritical, "Needs Data!")


DoCmd.GoToRecord , , acNewRec
Exit_New_Click:
Exit Sub
Err_New_Click:
MsgBox Err.Description
Resume Exit_New_Click

End Sub
Private Sub Save_Click()
On Error Resume Next
OkOnly = MsgBox("Are you sure you want to save these values?", vbOKCancel + vbCritical, "Your results will be entered.")



DoCmd.DoMenuItem acFormBar, acRecordsMenu, acSaveRecord, , acMenuVer70
Exit_Save_Click:
Exit Sub
Err_Save_Click:
MsgBox Err.Description
Resume Exit_Save_Click

End Sub
Private Sub Close_Click()
On Error Resume Next
YesNo = MsgBox("Are you sure you want to close?", vbYesNo + vbCritical, "This will close the form.")
If vbNo Then
DoCmd.Click_Close = False
End If





DoCmd.Close
Exit_Close_Click:
Exit Sub
Err_Close_Click:
MsgBox Err.Description
Resume Exit_Close_Click

End Sub


Here is the whole code just in case.

OBP
05-21-2010, 09:08 AM
In my code it has to be on One Line, did you put it on 1 line?
as in
If YesNo = vbNo Then Exit Sub

FF Ethan
05-21-2010, 09:10 AM
Right after Sub Close_Click?

OBP
05-21-2010, 09:32 AM
Like this
YesNo = MsgBox("Are you sure you want to close?", vbYesNo + vbCritical, "This will close the form.")
If YesNo = vbNo Then Exit Sub
DoCmd.Close
Exit_Close_Click:
Exit Sub

FF Ethan
05-21-2010, 09:44 AM
Perfect thank you!

geekgirlau
05-23-2010, 06:27 PM
There's no need to have a variable to store the result of the msgbox unless you want to use it multiple times.

Private Sub Close_Click()
If vbYes = MsgBox("Are you sure you want to close?", vbYesNo + vbCritical, "This will close the form.") Then
DoCmd.Close
End If
End Sub


Also I'm not sure what your other events are supposed to be doing - you're asking the user a question, then doing the same thing regardless of the answer.

FF Ethan
05-24-2010, 08:26 AM
geekgirlau,
Your absolutely right I didnt notice that before thank you for pointing that out! The ways that I've used work but will they work the same way for the other MsgBox's?

geekgirlau
06-02-2010, 03:04 AM
I'm not following the logic:


Private Sub Close_Click()
On Error Resume Next
YesNo = MsgBox("Are you sure you want to close?", vbYesNo + vbCritical, "This will close the form.")
' this is NOT checking the result of your message box
If vbNo Then
DoCmd.Click_Close = False
End If
' regardless of the answer to the question, the form will be closed
DoCmd.Close
Exit_Close_Click:
Exit Sub
Err_Close_Click:
MsgBox Err.Description
Resume Exit_Close_Click
End Sub