Using the In-line If (IIF) Function |
The traditional way to add conditional execution to one or more functions is with If and Else statements. A relatively simple alternative is the In-line If function (abbreviated IIF). The IIF function provides much of the functionality available with If and Else statements, but it doesn't require the use of multiple lines of Visual Basic. In fact, it is the only way to add conditional execution to a single-line expression. It is also easy enough to work with that many users will use it when possible in multi-line scripts, even though they could have used 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 three expressions:
A conditional expression.
An expression to be executed if the conditional expression is True.
An expression to be executed if the conditional expression is False.
Only one of the two expressions ("ExpressionIfTrue" or "ExpressionIfFalse") will get executed each time the IIF function is executed. 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 see if it is True or False. Examples of a conditional expression include:
Quantity > 100. This sample conditional expression is True if the variable named Quantity is greater than 100.
(Field("PartType") = "Breakable") AND (Field("Price") > 1000). This sample conditional expression examines the field named "PartType" in a database being read from and also looks at the "Price" field for the same part. In this case, the expression will evaluate to "True" if the PartType field for the current database record is the text "Breakable" and the Price of the part exceeds 1000.
Related Topics