Using the Switch Function to open Forms based on Criteria (1 Viewer)

LHolden

Registered User.
Local time
Today, 13:01
Joined
Jul 18, 2012
Messages
73
Hi all!

I recently designed a number of reports for a company to use as invoices. They run the invoices quarterly so there are 4 separate reports, 1 for each quarter. I wrote code for a button on a form which will run a specific report based on what month the button is clicked in (i.e. if it is clicked in April, the invoice for quarter 1 will be run). To do this I used IF logic:

If MonthName(Month(Date))="April" Then
DoCmd.OpenReport "Quarter1"

And so on. I know that the switch function is better to use than ifs, but I'm not sure how to write what I have above into a switch function, or if it is possible. Any help is appreciated!

Thanks in advance.
 

Mr. B

"Doctor Access"
Local time
Today, 12:01
Joined
May 20, 2009
Messages
1,932
Try using the Select Case method. You can specify multiple criteria and thus cover the months of each quarter with three case options in the Select Case statement like this:

Code:
Dim MonthName As String
MonthName = Format(Month(Date), "MMM")
Select Case MonthName
    Case "Jan", "Feb", "Mar"
        DoCmd.OpenReport "Quarter1"
    Case "Apr", "May", "Jun"
        DoCmd.OpenReport "Quarter2"
    Case "Jul", "Aug", "Sep"
        DoCmd.OpenReport "Quarter3"
    Case "Oct", "Nov", "Dec"
        DoCmd.OpenReport "Quarter4"
End Select
::
 

Users who are viewing this thread

Top Bottom