Yes/No Options on a MsgBox

  • Thread starter Thread starter CTJ
  • Start date Start date
C

CTJ

I need to understand how when creating a vbYesNo message box, how I assign
the next part of the code dependent on the Yes/No answer.

Simply put, from a command button, the YesNo message presents itself. On
clicking yes I want to Close the form without saving any record. On No, I
want to leave the form open.

My code is as follows:

MsgBox "Are you sure you want to close this Form?", vbYesNo, "Form will
close......?"
If vbNo Then
DoCmd.CancelEvent
Else
DoCmd.Close
End If

When I click no, the form stays open, when I click yes, the form stays open.

Thanks for assistance
 
On Mon, 5 Oct 2009 07:03:07 -0700, CTJ <[email protected]>
wrote:

You need to branch based on the return value of MsgBox:
if Msgbox(...) = vbYes then
'do this
else
'do that
end if

-Tom.
Microsoft Access MVP
 
If MsgBox ("Are you sure you want to close this Form?", vbYesNo, "Form will
close......?") = vbYes then
DoCmd.Close
Else
' do whatever
End If
 
If you put the msgbox function arguments in parrens "(...)" the
function will return the users response:

intResponse=msgbox("Your message","Your Title",vbYesNo)
select case intResponse
case vbNo
code for No
case vbYes
code for yes
end select



I need to understand how when creating a vbYesNo message box, how I assign
the next part of the code dependent on the Yes/No answer.

Simply put, from a command button, the YesNo message presents itself. On
clicking yes I want to Close the form without saving any record. On No, I
want to leave the form open.

My code is as follows:

MsgBox "Are you sure you want to close this Form?", vbYesNo, "Form will
close......?"
If vbNo Then
DoCmd.CancelEvent
Else
DoCmd.Close
End If

When I click no, the form stays open, when I click yes, the form stays open.

Thanks for assistance
THANKS!
David G.
 
iletisinde şunu yazdı said:
I need to understand how when creating a vbYesNo message box, how I assign
the next part of the code dependent on the Yes/No answer.

Simply put, from a command button, the YesNo message presents itself. On
clicking yes I want to Close the form without saving any record. On No, I
want to leave the form open.

My code is as follows:

MsgBox "Are you sure you want to close this Form?", vbYesNo, "Form will
close......?"
If vbNo Then
DoCmd.CancelEvent
Else
DoCmd.Close
End If

When I click no, the form stays open, when I click yes, the form stays
open.

Thanks for assistance
 
IF MsgBox ("Are you sure you want to close this Form?", vbYesNo, "Form will
close......?" = VbNO then
DoCmd.CancelEvent
Else
DoCmd.Close
End If

Or

Dim iResult as Long

iResult = MsgBox ("Are you sure you want to close this Form?", vbYesNo, "Form
will close......?")

If iResult = vbNo then
DoCmd.CancelEvent
Else
DoCmd.Close
End If

John Spencer
Access MVP 2002-2005, 2007-2009
The Hilltop Institute
University of Maryland Baltimore County
 
Whoops forgot the closing parenthesis on the first example.

IF MsgBox ("Are you sure you want to close this Form?", vbYesNo, "Form will
close......?") = VbNO then

John Spencer
Access MVP 2002-2005, 2007-2009
The Hilltop Institute
University of Maryland Baltimore County
 
Back
Top