Microsoft Access IIF Function
An If / Then Alternative For VBA and Query Design
Q: I want to create a query that will calculate sales
tax, but ONLY for customers in my home state of NY. For other customers,
I need the tax to show up as zero. How can I do this?
A: The easiest way to do this
would be with the IIF function. This one function acts like an IF / THEN
statement in Visual Basic. Here's the basic syntax:
=IIF(Condition,
Value If True, Value
If False)
For example, if I say:
=IIF(State="NY",
"Yes", "No")
then the IIF function will return the word "Yes" if the State field
equals "NY". Otherwise, it returns "No."
You could also use it to return the actual tax rate, like this:
=IIF(State="NY",
0.875, 0)
In this case, if the State is "NY" then the tax rate is returned, which
is 0.875 (or 8.75%), otherwise a zero is returned.
If you want to use IIF in a query, just create a blank new "calculated
field" out of it, like this:
TaxRate: IIF(State="NY",
0.875, 0)
Here's a screen shot from one of my tutorials:
Here is a FREE VIDEO TUTORIAL
that will walk you through using the IIF function in detail.
I teach the concepts in this free mini tutorial in my
Access 207 course. These are
full courses - over 60 minutes in length each. I spend a lot more time going
over how to do this, in addition to lots more tips and tricks.
UPDATE: I covered the IIF function again, in more detail, in my
Access 2013 Expert Level 8
class.
By Richard Rost
Click here to sign up for more FREE tips
|