IIF is a great function in Access that can be used almost anywhere and provides you with capabilities in your code you may not be using. Many will argue it can make your code harder to read, (it does), this post is not here to debate that, but rather provide you examples of it’s flexibility.

MS Access Report Generator

First, the definition of what it does:

The function will return the “Value_If_True” portion if Condition is True, otherwise it will return “Value_If_False”.

Variable Assignment

You may use it in place of an IF block, for example when assigning a value to a variable:

In the above example, if Date() returns 10/1/09 as today’s date then the variable strOutcome will be assigned the value “Good”, otherwise it will get assigned “Bad”.

Building Strings

You can also use it when building strings in your code:

If the variable strUser is Juan, the string will look like this:

Otherwise, if its anything else, for example “Joe”:

Power tip: Think of the true or false options as areas to insert code. You can perform complex assignments in both regions:

Notice the code in the True portion:  “User: ‘” & strUser & “‘”, it’s building the string inside another string!

Invoking Commands

Here’s a great way to use IIF when invoking a command:

If the user is “Juan” the form will open in a normal window, otherwise it will open as a dialog.

To see how IIF can be used in a query, click here.