Yes/No Options on a MsgBox

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
 
T

Tom van Stiphout

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
 
R

RonaldoOneNil

If MsgBox ("Are you sure you want to close this Form?", vbYesNo, "Form will
close......?") = vbYes then
DoCmd.Close
Else
' do whatever
End If
 
D

David G.

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.
 
M

murti

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
 
J

John Spencer

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
 
J

John Spencer

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top