result of vbYesNo

  • Thread starter Thread starter Andreas
  • Start date Start date
A

Andreas

I use the vbYesNo in a msgbox, like:

MsgBox ("Are you sure ?", vbYesNo, "Confirm")

How do i retrieve the answer then? What is the reference name to retrieve
the selected yes or no?
 
Andreas said:
I use the vbYesNo in a msgbox, like:

MsgBox ("Are you sure ?", vbYesNo, "Confirm")

How do i retrieve the answer then? What is the reference name to
retrieve the selected yes or no?

Either..

If MsgBox ("Are you sure ?", vbYesNo, "Confirm") = vbYes Then...

....or...

SomeVariable = MsgBox ("Are you sure ?", vbYesNo, "Confirm")

If SomeVariable = vbYes Then...
 
Andreas,
See Help under MsgBox Function...
In order to determine which MsgBox button has been selected, you need to equate a
variable to the MsgBox.

Dim Response as String
Response = MsgBox("Select Yes or No", vbYesNo, "Some Title")
If Response = vbYes Then
' do something....
Elseif Response = vbNo
' do something else
End If

vbYes also has a value of 6 and vbNo has a value of 7, so Response could also be
queried as to = 6 or = 7, but using vbYes and vbNo makes the code much easier to read.

--
hth
Al Campagna
Candia Computer Consulting
Microsoft Access MVP - Candia, NH USA
http://home.comcast.net/~cccsolutions

"Find a job that you love, and you'll never work a day in your life."
 
Al Campagna said:
Andreas,
See Help under MsgBox Function...
In order to determine which MsgBox button has been selected, you
need to equate a variable to the MsgBox.

Dim Response as String
Response = MsgBox("Select Yes or No", vbYesNo, "Some Title")

Response should be declared as Integer, not String, since that's the
type that the MsgBox function returns.
 
Dirk,
That's odd... I've always used String for vbYes or vbNo, and Integer for =6 or =7.
I tend to agree, but... here's the example from Help for MsgBox...
(I removed some extraneous "Help" code)

Dim Msg, Style, Title, Response, MyString
Msg = "Do you want to continue ?"
Style = vbYesNo + vbCritical + vbDefaultButton2
Title = "MsgBox Demonstration"
Response = MsgBox(Msg, Style, Title)
If Response = vbYes Then
MyString = "Yes" ' Perform some action.
Else
MyString = "No" ' Perform some action.
End If

Also, I tried these two examples, using String and Integer in each instance... and they
all work...

Dim Response As String '(or Integer)
Response = MsgBox("Yes or No", vbYesNo)
If Response = vbYes Then
Beep
End If

Dim Response As String '(or Integer)
Response = MsgBox("Yes or No", vbYesNo)
If Response = 6 Then
Beep
End If

Al Campagna . Candia Computer Consulting . Candia, NH USA
Microsoft Access MVP
http://home.comcast.net/~cccsolutions

"Find a job that you love, and you'll never work a day in your life."
 
Dirk,
On second thought, I guess the Help example is using the Variant type, because no type
is specified, but String does "appear" to still work...
I agree, it "should" fail...
Al
 
Al Campagna said:
Dirk,
On second thought, I guess the Help example is using the Variant
type, because no type is specified, but String does "appear" to still
work... I agree, it "should" fail...

It won't fail, but it's doing unnecessary conversions: first when the
numeric return value is assigned to a string variable, and then again
when the string variable is compared to the numeric constant vbYes or
vbNo. In the case of a comparison "If (string) = (integer)", I'm not
sure which side is converted, but plainly one of them has to be.
 
Dirk,
I'm really surprised that Access would do/allow that...
But, you're right... work or not, vbYes and 6 are both integers, and should be treated
as such.
Thanks Dirk,
Al
 

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

Back
Top