Using the In-line If (IIF) Function |
The traditional way to configure one or more functions to run conditionally is by using If and Else statements. A relatively simple alternative is the In-line If (IIF) function. The IIF function provides much of the functionality that is available with If and Else statements, but it does not require you to use multiple lines of Visual Basic. In fact, the IIF function is the only way to configure a single-line expression to run conditionally. It is also easy enough to use that many users use it when possible in multi-line scripts, even though they could use separate If and Else statements.
The IIF function syntax is as follows.
IIF(<ConditionalExpression>,<ExpressionIfTrue>,<ExpressionIfFalse>)
|
Although the IIF function is available in a number of computer languages, it is not native to Visual Basic. |
The IIF function contains the following expressions:
A conditional expression
An expression that is run if the conditional expression is True
An expression that is run if the conditional expression is False.
Only one of the two expressions ("ExpressionIfTrue" or "ExpressionIfFalse") is run each time the IIF function runs. Either of these expressions can be any valid single line expression, including additional ("nested") IIF functions.
The IIF function first examines the conditional expression (the first term in the function) to determine whether it is True or False. Examples of a conditional expression include the following:
Quantity > 100: This sample conditional expression is True if the Quantity variable is greater than 100.
(Field("PartType") = "Breakable") AND (Field("Price") > 1000): This sample conditional expression examines the PartType field in a database and the "Price" field for the same part. In this case, the expression evaluates to True if the PartType field for the current database record is the "Breakable" text string and the Price field exceeds 1000.
Related Topics