totn Access Functions

MS Access: iif Function

This MSAccess tutorial explains how to use the Access iif function with syntax and examples.

Description

The Microsoft Access iif function returns one value if a specified condition evaluates to TRUE, or another value if it evaluates to FALSE.

Syntax

The syntax for the iif function in MS Access is:

iif ( condition, value_if_true, value_if_false )

Parameters or Arguments

condition
The value that you want to test.
value_if_true
The value that is returned if condition evaluates to TRUE.
value_if_false
The value that is return if condition evaluates to FALSE.

Returns

The iif function returns value_if_true when the condition is TRUE.
The iif function returns value_if_false when the condition is FALSE.

Applies To

The iif function can be used in the following versions of Microsoft Access:

  • Access 2019, Access 2016, Access 2013, Access 2010, Access 2007, Access 2003, Access XP, Access 2000

Example

Let's look at how to use the iif function in MS Access:

iif ([Qty] > 10, "large", "small")

This example would return "large" if the value in the Qty field is greater than 10. Otherwise, it would return "small".

This is equivalent to the following IF statement in VBA code.

If [Qty] > 10 Then
   result = "large"
Else
   result = "small"
End If

Example in SQL/Queries

You can use the iif function in a query in Microsoft Access.

For example:

Microsoft Access

In this query, we have used the iif function as follows:

Hours: IIf([Time Out]<#12:00:00 PM#,([Time Out)-[Time In])*24,(([Time Out]-[Time In])*25)-0.5)

In this example, if the [Time Out] field is less than or equal to 12 o'clock noon, then the iif function will return the number of hours that have elapsed between [Time Out] and [Time In].

If [Time Out] is greater than 12 o'clock noon, then the iif function will subtract 30 minutes from the time worked.

The results of the iif function will be displayed it in a column called Hours.

Frequently Asked Questions


Question: How would I use the iif function if I wanted to have more than one condition?

Answer: You could use the AND keyword to include multiple conditions.

Microsoft Access

In the example above, the iif function will return "Yes" if both the ContactTitle = "Owner" and City = "Madrid". If one or both of these conditions is not met, it will return "No".