vbYesNoCancel Open Report on Yes

flemmo

Registered User.
Local time
Today, 14:49
Joined
Apr 26, 2006
Messages
69
I have the following code:

Code:
Answer = MsgBox("My prompt?", vbYesNoCancel, "Title")

If Answer = vbYes Then
     DoCmd.OpenReport MyReport, acPreview, "MyReportFilter"
ElseIf Answer
     DoCmd.OpenReport MyReport2, acPreview, "MyReportFilter2"
End If

However, when I click on Yes or No, no report loads. What am I doing wrong?
Thanks
 
Elseif <Expression>

The expression needs to be something that is or evaluates to a True or False. Only a True value causes the if to be "entered", true equals to a -1.
Your answer is vbNo, the numeric contained within this variable is 7... i.e. <> -1 => Does not equal True => No entering the ElseIf.

I hope you understand and can fix the problem now.
 
Change it to:

If MsgBox("My prompt?", vbYesNoCancel, "Title") = vbYes Then
DoCmd.OpenReport MyReport, acPreview, "MyReportFilter"
Else
DoCmd.OpenReport MyReport2, acPreview, "MyReportFilter2"
End If
 
Also, are MyReport and MyReport2 the actual names of the reports? If so they need to be in quotes:

Code:
Answer = MsgBox("My prompt?", vbYesNoCancel, "Title")

If Answer = vbYes Then
     DoCmd.OpenReport "MyReport", acPreview, "MyReportFilter"
Else
     DoCmd.OpenReport "MyReport2", acPreview, "MyReportFilter2"
End If
 
Also, are MyReport and MyReport2 the actual names of the reports? If so they need to be in quotes:

They are named more appropriately, but you're quite right I missed the quotes out, silly me.
 

Users who are viewing this thread

Back
Top Bottom