IIf

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

When using an IIf calculation in a form, can multiple conditions be used or
is there a limitation to 2?
 
The IIf only has 2 options. The first is for the True condition, the second
is for the false. Be careful using calculations in an IIf statement. Access
evaluates both the True and the False statements before it returns a value.
If either would produce an error, you will get the error.

I recommend you not use an IIf for a calculation with one exception. the
IIf is best used to return a value: IIf(X>Y,10,Z)
The exception is for checking for a divide by zero problem before it creates
an error:

X = Y/IIf(Z=0,1,Z)
Notice it is not actually a calculation, but used in a calculation.
 
you can use "nested" IIf() functions, but you have to think the logic
through carefully or you may find yourself coming up with unexpected
results. for instance, the logic path can be

If this is true, then do this, otherwise
If this is true, then do this, otherwise
If this is true, then do this, otherwise do this

IIf(this is true, x, IIf(this is true, y, IIf(this is true, z, a)))

or perhaps

If this is true, then
If this true, then do this, otherwise do this
otherwise do this

IIf(this is true, IIf(this is true, x, y), z)

as you can see, nesting can quickly become very complex. and you need to be
careful to include an Else clause in every IIf() function, or you could end
up with a "dangling" calculation that returns no value. there are "related"
functions Choose() and Switch() that you may find useful (see Access Help
for details), or you may want to consider writing a custom function to
process the data and return the value you need to the query.

hth
 
Thanks everybody! I think you've all giving me the answers I needed! VERY
much appreciated!
 
Back
Top