Access: IIF Function

In Access, the IIF function returns one of two arguments depending on the evaluation of an expression.

The syntax for the IIf function is:

IIf ( expression, truepart, falsepart )

IIf always evaluates both truepart and falsepart, even though it returns only one of them. Because of this, you should watch for undesirable side effects. For example, if evaluating falsepart results in a division by zero error, an error occurs even if expr is True.

expression is the value that you want to test.

truepart is the value returned if expression evaluates to TRUE.

falsepart is the value returned if expression evaluates to FALSE.

Example

IIf ([Price] > 400, "expensive", "cheap")      Returns "expensive" if the value in the Price field is
                                               greater than 400. Otherwise, returns "cheap". 

VBA Code

If [Price] > 400 Then
result=”expensive”
Else
result=”cheap”
End If

This is equivalent to the following IF statement in VBA code. The preceding example uses the IIf function to evaluate the givven expression and returns the word “Expensive” if the amount is greater than 400; otherwise, it returns the word “Cheap”.

SQL query

You can also use the iif function in a query.

SELECT SellerID, BuyerID, Item, IIf([Price]>400,[Price]*1.05,[Price]*1.15) AS Total
FROM Antiques 
admin

admin

Leave a Reply

Your email address will not be published.