Select Case if true (1 Viewer)

ClaraBarton

Registered User.
Local time
Today, 02:46
Joined
Oct 14, 2019
Messages
463
Code:
Select Case Me!togMonth
    
    Case Me!togMonth = True
       Me.Visible = False
            DoCmd.OpenReport "rptMonth", _
                View:=acViewPreview, _
                OpenArgs:=Me.name
            Exit Sub
  
    Case Me!togMonth = False
         DoCmd.OpenForm _
               formName:="popPrint", _
               View:=acNormal, _
               Windowmode:=acDialog
        
            'picked your report so...
            If IsNull(TempVars!Report) Then
                Exit Sub
            ElseIf TempVars!Report = "rptWeek" Then
                Me.Visible = False
                GroceryListTable
            End If
In the above code, debug returns Me!togMonth as false and yet the code continues to the next line instead of going to Me.togMonth=False. Am I doing something obviously wrong?
 

theDBguy

I’m here to help
Staff member
Local time
Today, 02:46
Joined
Oct 29, 2018
Messages
21,473
With only two options, you could just use an If/Then/Else block. I think the reason your Select Case block is not working is because it's probably not constructed correctly.
 
Last edited:

ClaraBarton

Registered User.
Local time
Today, 02:46
Joined
Oct 14, 2019
Messages
463
I'm assuming that also. That's why I asked.
 

GaP42

Active member
Local time
Today, 19:46
Joined
Apr 27, 2020
Messages
338
Try altering your CASE statement - you have said
Select Case Me!togMonth
Then have
Case Me!togMonth = True
should you not just be testing CASE True and later Case False
 

MajP

You've got your good things, and you've got mine.
Local time
Today, 05:46
Joined
May 21, 2018
Messages
8,529
um... isn't that what I'm doing?
No
Code:
Select Case Me!togMonth
    Case Me!togMonth = True
    Case Me!togMonth = False
end select
to
Code:
Select Case Me!togMonth
    Case True
    Case False
end select
 

cheekybuddha

AWF VIP
Local time
Today, 10:46
Joined
Jul 21, 2014
Messages
2,280
Although not really required for this scenario, you can also do:
Code:
Select Case True
    Case Me!togMonth = True
    Case Me!togMonth = False
    Case IsNull(Me.togMonth)    ' if your control is TripleState
end select
 

The_Doc_Man

Immoderate Moderator
Staff member
Local time
Today, 04:46
Joined
Feb 28, 2001
Messages
27,186
Clarity to you was in this case overkill (of syntax) and for literal-minded computer programs like the VBA compiler, overkill kills.

However, once you look at the correct syntax, you should realize that your expressions would not evaluate correctly. This is what you REALLY were evaluating. I'm going to add two lines of code to show what REALLY happens with that statement.

Code:
Select Case Me!togMonth
    Case Me!togMonth = True
        X = 1
    Case Me!togMonth = False
        X = 2
end select

Your CASE statements present an expression following keyword CASE, but CASE wants a single scalar value, not an expression. That means it must evaluate the expressions to form those scalars. This conversion is a key point.

Let's execute the SELECT CASE statement, where Me!togMonth is either TRUE or FALSE (not counting David's point about the NULL option.) Then let's see what happens.

In the case where Me!togMonth was TRUE, then the first CASE statement (CASE Me!togMonth=TRUE) resolves to CASE TRUE. So since Me!togMonth was TRUE, X=1 will be executed. The other CASE statement (Me!togMonth = False) resolves to CASE FALSE but that doesn't matter because the CASE ladder only executes the first matching CASE value statement anyway, and we already hit that.

In the case where Me!togMonth was FALSE, the first CASE statement (CASE Me!togMonth=TRUE) resolves to CASE FALSE. Since Me!togMonth was FALSE, X=1 will be executed. The other CASE statement (CASE Me!togMonth=FALSE) resolves to CASE TRUE, which again doesn't mater because the CASE ladder already found its matching value.

Which means in a nutshell that you would never have executed the segment where I set X=2.
 
Last edited:

ebs17

Well-known member
Local time
Today, 11:46
Joined
Feb 7, 2020
Messages
1,946
A check box can have a third state (=> NULL). This should not be left unattended.
Code:
Select Case Me!togMonth
    Case True
    Case False
    Case Else
       ' unexpected case
End Select
 

Pat Hartman

Super Moderator
Staff member
Local time
Today, 05:46
Joined
Feb 19, 2002
Messages
43,275
In the expression:
SomeField = True
VBA will interpret this as True as long as SomeField is not null. That is why, the first case is being selected. You are not checking the value of SomeField as the code is written, you are checking the value of "SomeField = True"
@MajP gave you the correct syntax.
 

Users who are viewing this thread

Top Bottom