# Access - Nested IIf Statements



## Devan (Feb 10, 2005)

I am looking for an example of how to do a nested iif statement in access. I have 7 separate returns from a field in the database I am using, if I get e.g 5_Bro returned I want to enter brown, if I get 6_Yel I want to enter Yellow automatically. I can get this to work if I do separate If statements but I only want to only have 1 coloum which returns the correct colour. Or any other solution to this problem if not an if statement


----------



## coachdan32 (Nov 13, 2003)

The second IIf goes in place of the Else condition.

Example:
=If([field1]=1,1,"")
Nested:
=If([field1]=1,1,If([field1]=2,2,""))

And so on...There is a limit on the level of nestings - not sure of how many though.


----------



## WhitPhil (Oct 4, 2000)

Would the Switch Function do what you want?


----------



## Devan (Feb 10, 2005)

coachdan32 said:


> The second IIf goes in place of the Else condition.
> 
> Example:
> =If([field1]=1,1,"")
> ...


Thanks so much, looks so simple now I have the answer. It works find I just tested it with a couple of options. I may have a look at the switch statement as welll


----------



## HeavyBall (Oct 29, 2004)

select may even work better:

Select case [oldcolorfield]
Case brn
[newcolorfield] = "brown"
Case yel
[newcolorfield] = "yellow"
End Select


----------



## coachdan32 (Nov 13, 2003)

I agree with HeavyBall, a case select would seem to be a better fit.


----------



## Devan (Feb 10, 2005)

Thank you I will try this out as well, always good to a few options to hand!


----------

