Nested IIf Branches

C

croy

When nesting IIf statements, it seems to me that subsequent
IIf's always end the previous IIf. In other words, there
cannot be another IIf statement in both the "then" and
"Else" parts of the first IIf.

Is that true?
 
D

Dale Fye

No, you can nest IIF() statements inside either result of an if. For example

IIF(A < B, IIF(B < C, "C", "B"), IIF(A < C, "C", "A"))

Will tell you which of the three variables is larger. But nesting IIF
statements can get very complicated, so when I write on out, I generally try
to write it like:

IIF(A < B, _
IIF(B < C, _
"C", _
"B"), _
IIF(A < C, _
"C", _
"A") _
)

This helps me make sure I have all of the pieces where they ought to be, and
the parenthesis as well. Then, I get rid of the line break characters while
trying to keep it readable.

When I get to the point where the nesting is hard to read, I create a
function and pass it the values. That way, I can actually document what is
going on so that someone reading the code or query can actually determine
what I'm trying to accomplish.


--
HTH
Dale

Don''t forget to rate the post if it was helpful!

email address is invalid
Please reply to newsgroup only.
 
C

croy

No, you can nest IIF() statements inside either result of an if. For example

IIF(A < B, IIF(B < C, "C", "B"), IIF(A < C, "C", "A"))

Will tell you which of the three variables is larger. But nesting IIF
statements can get very complicated, so when I write on out, I generally try
to write it like:

IIF(A < B, _
IIF(B < C, _
"C", _
"B"), _
IIF(A < C, _
"C", _
"A") _
)

This helps me make sure I have all of the pieces where they ought to be, and
the parenthesis as well. Then, I get rid of the line break characters while
trying to keep it readable.

When I get to the point where the nesting is hard to read, I create a
function and pass it the values. That way, I can actually document what is
going on so that someone reading the code or query can actually determine
what I'm trying to accomplish.


It sure does! Thanks.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top