Generate Report Using If...ElseIf...End If Construct

AlexB123

Board Regular
Joined
Dec 19, 2014
Messages
207
Hi all,

I'm making some changes to the images that go out on certain reports. I have a report that is tied to a combobox of client ID's and produces a single report but has an image I want to change. I need to produce a second report, with a different logo, if the client ID is equal to one of two values.

First I tried:

Code:
 'If statement to allow for new rptLettersSBD when ClientID = ABC001 or ABC002
        If ((Forms!frmLetter.cmbClientID = "ABC001") Or (Forms!frmLetter.cmbClientID = "ABC002")) Then
                DoCmd.OutputTo acOutputReport, "rptLetterSBD", acFormatPDF, PDFFileName, False
        Else
                DoCmd.OutputTo acOutputReport, "rptLetterBranded", acFormatPDF, PDFFileName, False
        End If

Then I tried:
Code:
'If statement to allow for new rptLettersSBD when ClientID = ABC001 or ABC002
        If Forms!frmLetter.cmbClientID = "ABC001" Then
                DoCmd.OutputTo acOutputReport, "rptLetterSBD", acFormatPDF, PDFFileName, False
        ElseIf Forms!frmLetter.cmbClientID = "ABC002" Then
                DoCmd.OutputTo acOutputReport, "rptLetterSBD", acFormatPDF, PDFFileName, False
        Else
                DoCmd.OutputTo acOutputReport, "rptLetterBranded", acFormatPDF, PDFFileName, False
        End If

The first code block successfully produced a report for the first ClientID. My database crashed when I used the second code block to try and get the report for the second ClientID.

Thanks for any assistance!
 
Last edited:

Excel Facts

Links? Where??
If Excel says you have links but you can't find them, go to Formulas, Name Manager. Look for old links to dead workbooks & delete.
I would guess the crash is not related to the use of If-elseif-else-end here. That is perfectly good vba syntax as far as I can tell.
 
Upvote 0
I am not a big fan of "ElseIf". I often get confused on whether or not I need an "End If" with it. For multiple options, I prefer CASE statements, i.e.
Code:
 'If statement to allow for new rptLettersSBD when ClientID = ABC001 or ABC002
        Select Case Forms!frmLetter.cmbClientID
            Case "ABC001", "ABC002"
                DoCmd.OutputTo acOutputReport, "rptLetterSBD", acFormatPDF, PDFFileName, False
            Case Else
                DoCmd.OutputTo acOutputReport, "rptLetterBranded", acFormatPDF, PDFFileName, False
        End Select
If you have other conditions, you can add more Cases.
See: https://www.techonthenet.com/excel/formulas/case.php
(though the link is for Excel VBA, it works exactly the same way in Access VBA).
 
Last edited:
Upvote 0
Xenou, thank you for looking over the syntax.

I found that when I reloaded the form everything worked. There still seem to be kinks related to queries and such, so I appreciate the feedback.

Joe4, thank you for the suggestion! I, too, prefer case statements, and will try that out.
 
Upvote 0

Forum statistics

Threads
1,214,606
Messages
6,120,488
Members
448,967
Latest member
visheshkotha

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top