Select case using NOT operator

arage

Registered User.
Local time
Today, 01:50
Joined
Dec 30, 2000
Messages
537
Select case using NOT operator
I’d appreciate if somebody could comment on the below code. It doesn’t execute for a value that sets the case to true. Instead it exits to the calling procedure.

GlobalUserCategory is a string and the calling procedures error handler itdentifies the error as 13 a type mismatch.

Code:
            'create regionCombo based on user (see case 4)...
            Select Case GlobalUserCategory
                Case Not "Dir" Or Not "Mgr" Or Not "Coord"
 
Keep in mind De Morgan's Theorem.

(!A OR !B OR !C) = !(A and B and C)

Distributing a Not (!) inverses the logic clause. Therefore, only one has to be false to return true. All must be true to return false. Also, use the Is <>

Select Case GlobalUserCategory
Case Is <> "Dir", Is <>"Mgr", Is <> "Coord"

In this case, one of them is always going to be true, so ...
 
Last edited:
Yup that did it.
 
Last edited:
Well this is kind of confusing to me b/c if say “Dir” is chosen now, it will execute the statement which is wrong.
 
The case statement is still doing an OR. Try the following:

Code:
Select Case GlobalUserCategory
    Case  "Dir", "Mgr", "Coord" 
        ' do what you want when category is any of these values
    Case Else
        'do what you want for all the other values
End Select

Case statements do not need to be followed by action statements. Having no action in the first case is fine.

pdx_man explained the problem with or and not.

A simple If statement would be:
If GlobalUserCategory <> "Dir" AND GlobalUserCategory <> "Mgr" AND GlobalUserCategory <> "Coord" Then
...
End If

Another alternative is:
If GlobalUserCategory NOT In("Dir", "Mgr", "Coord") Then

When conditions are connected with the OR operator, only one of them needs to be true for the whole condition to be true. Since the same field cannot be both "Dir" and "Mgr" at the same time, it is either NOT "Dir" or NOT "Mgr". Because "Dir" is not equal to "Mgr". One side of the or or the other will ALWAYS be true. When you connect the conditions with AND, both statements need to be true. The field must not be "Mgr" and it also must not be "Dir'.

English is pretty confusing when it comes to conditional statements regarding the combination of AND, OR, and NOT operators. You need to understand a little boolean logic to get these conditions straight. That's what computers use and it is much more precise than plain ol' English.
 

Users who are viewing this thread

Back
Top Bottom