IF-ELSE Conditional Operators for Simple Queries?

  • Thread starter Thread starter lambo
  • Start date Start date
L

lambo

Guest
Hi

I will try illustrate this problem which I'm stuck with using pseudocode. There are 4 fields in a table; Choose_Type, AAA, BBB and Final_Value. I'm not sure if I have to code it using VBA, as I'm new to using MAccess.

If (Choose_Type = 1) Return (AAA) // Final_Value == AAA
If (Choose_Type = 2) Return ( BBB) // Final_Value == BBB
Else Return (0)

Is there a way to implement the above pseudocode using simple Queries (SQL or in normal query design view) ? Any advise would be of great help.

Thanks.
 
Not sure if this is what you meant but:

Result: iif([Condition 1] = [AAA],[Show Result],iif([Condition2] = [BBB],[Show Result],0))
 
If (Choose_Type = 1) Return (AAA) // Final_Value == AAA
If (Choose_Type = 2) Return ( BBB) // Final_Value == BBB
Else Return (0)

Try the SWITCH structure, much like the CASE structure in SQL.

SELECT SWITCH (choose_type = 1,"AAA",choose_type=2,"BBB",choose_type NOT EXISTS ("AAA", "BBB"), 0) AS Final_Value

Im not sure if SWITCH has an ELSE component. I got around it as above or you could use a Subquery.

Id do it this way for scalability reasons. An IIF statement can get a bit confusing.
 
Hi

Thanks for the solutions meboz and birdy! At least now I know there are commands which makes this operation possible. It works great now :)
 

Users who are viewing this thread

Back
Top Bottom