Need two separate answers from an IF statement

  • Thread starter Thread starter Garyj
  • Start date Start date
G

Garyj

Guest
I have an IF statement that returns the correct answer if the business user select "YES". However when clicking on the "NO" button nothing happens. How can I return a message of "That is incorrect" when clicking the "NO" button?

Thanks in advance

Private Sub Command144_Click()
If Msgbox("Every debt must have a debtor and every debtor must have a debt.", vbYesNo) = vbYes Then
Msgbox "You selected the correct answer."
End If
End Sub
 
Just like any If statement, you can use the ELSE option. So if vbYes is not selected then the only other option is vbNo. So:

If Msgbox("Every debt must have a debtor and every debtor must have a debt.", vbYesNo) = vbYes Then
Msgbox "You selected the correct answer."
Else
MsgBox "That is incorrect."
End If
 
Placing a title in the display

Thank you, just one more thing. Is there a way to place a title when the button is displayed?
 
Garyj said:
Thank you, just one more thing. Is there a way to place a title when the button is displayed?
Code:
Answer = MsgBox("message", vbYesNo, "Title")
title is a string, hence the '"'

QC
PS

this would change your code into:
Code:
Dim Answer as string
Answer = MsgBox("message", vbYesNo, "Title")

If Answer =VbYEs then 
 '... code if Answer =vbYes
Else
 '... code if Answer =vbNo
end if
 
Last edited:
If Msgbox("Every debt must have a debtor and every debtor must have a debt.", vbYesNo, "Question") = vbYes Then
Msgbox "You selected the correct answer.", vbExclamation, "Correct Answer"
Else
MsgBox "That is incorrect.", vbExclamation, "Incorrent Answer"
End If
 
Last edited:
Adrianna said:
If Msgbox("Every debt must have a debtor and every debtor must have a debt.", vbYesNo, "Question") = vbYes Then
Msgbox "You selected the correct answer.", vbExclamation, "Correct Answer"
Else
MsgBox "That is incorrect.", vbExclamation, "Incorrent Answer"
End If

haha forgot that one, :D

I got an error indicating an equal sign ("=") was expected,
didn't think "msgbox(..) = vbYes" :eek:

QC :p
 
When I entered the code it did not display correctly and the title did not show. What am I doing wrong.

Thanks again.
 
I tried it again with the following as a test:

If MsgBox("Is this working?", vbYesNo, "Question") = vbYes Then
MsgBox "This works so I don't know what the issue is", vbExclamation, "Answer"
Else
MsgBox "This didn't work...or did it", vbCritical, "Wrong Answer"
End If

Worked fine for me. Can you post your code here so that I can see exactly what you're trying to run. Then I can debug it and post back.

BTW...not the it probably matters, but what Version of Access are you running?
 
Orginal code works great.
If Msgbox("Does every debt have too a debtor and every debtor must have a debt?", vbYesNo) = vbYes Then
Msgbox "CORRECT!!!"
Else
Msgbox "INCORRECT!!!"
End If

I don't understand how the will work using the below structure. If you could help I would appreciate it.

Dim Answer as string
Answer = MsgBox("message", vbYesNo, "Title")

If Answer =VbYEs then
'... code if Answer =vbYes
Else
'... code if Answer =vbNo
end if
 
It's just a round about way of doing what I already posted. There is no reason that you can't use that method instaead, but I don't think it gives you everything that you wanted.

It would look like this:

Dim Answer As String

Answer = MsgBox("Every debt must have a debtor and every debtor must have a debt.", vbYesNo, "Experience with VBA")
If Answer = vbYes Then
MsgBox "CORRECT!!!"
Else
MsgBox "INCORRECT!!!"
End If
 

Users who are viewing this thread

Back
Top Bottom