Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Coding dilemma with Case statement 1

Status
Not open for further replies.

Maquis

Programmer
Jul 19, 2001
934
US
Hi there. I am trying to write a VBA routine within a report and I'm not quite sure how to write this. I checked Access's help file, but it wasn't much help.

I have 3 boolean variables. Let's call them X, Y and Z. I want to write a case statement to evaluate and process the 8 possibilities.

Code:
Select Case  <what goes here??>
   Case X is true, Y is True, Z is false
        do stuff
   Case X is false, Y is True, Z is false
        do stuff
   etc, etc
End Case

Since VB's case statement is structured to evaluate 1 variable, I'm not sure how to write this. Should I construct a string from the 3 variables and evaluate the string in the case? Should I forget the case and make 8 if statements? (Yuck!)

In the PICK/universe world that I'm used to, case statements work much differently.


Maq B-)
<insert witty signature here>
 
Hi!

Instead of using a Select statement, try an ElseIf:

If X is true, Y is True, Z is false Then

ElseIf X is false, Y is True, Z is false Then

etc.

As far as I can tell, the only other choice is to nest your select statements

Select Case X
Case False
Select Case Y
etc.

Personally I think the elseif looks better.

hth
Jeff Bridgham
 
Or, do the old assignment to the ^2 var.

If (X) Then
[tab]VarN = 1
End If

If (Y) then
[tab]VarN = VarN + 2
End If

If (Z) then
[tab]VarN = VarN + 4
End If

[tab]If (Y) then
[tab]VarN = VarN + 2
End If


Select Case VarN
[tab]Case = 1
[tab][tab]'do Stuff for &quot;X&quot; Only

[tab]Case = 2
[tab][tab]'do Stuff for &quot;Y&quot; Only

[tab]Case = 4
[tab][tab]'do Stuff for &quot;Z&quot; Only

[tab]Case = 5
[tab][tab]'do Stuff for &quot;X&quot; & Z&quot;

[tab]Case = 6
[tab][tab]'do Stuff for &quot;Y&quot; & Z&quot;

[tab]Case = 7
[tab][tab]'do Stuff for &quot;X&quot; & &quot;Y&quot; & &quot;Z&quot;

End Select

Better, Why do you have the three booleans? JUst make the VarN to begin with.



MichaelRed
mred@att.net

There is never time to do it right but there is always time to do it over
 
Thanks, Michael. I never thought of the 1,2,4 thing. I like that suggestion better than doing the Elseif or imbedded case (no offense Jeff, it's just a style preference).

To answer your question about having 3 booleans, I am doing other stuff with these variables besides this case statement, where I'll just be looking at 1 of them. It's just that I needed to combine all 3 for this one function. Maq B-)
<insert witty signature here>
 
Hi!

No offense taken. Honestly, I like Michael's suggestion better too! :)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top