IIF vs SWITCH Function in MS-Access
The IIF() Function definition is reproduced here from Microsoft Visual Basic for Applicatiions Reference below:
IIf Function Returns one of two parts, depending on the evaluation of an expression.
Syntax: IIf(expr, truepart, falsepart)
The IIf function syntax has these named arguments:
Part Description expr Required. Expression you want to evaluate. truepart Required. Value or expression returned if expr is True. falsepart Required. Value or expression returned if expr is False.
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.
This example uses the IIf function to evaluate the TestMe parameter of the CheckIt procedure and returns the word "Large" if the amount is greater than 1000; otherwise, it returns the word "Small".Function CheckIt(TestMe As Integer) CheckIt = IIf(TestMe > 1000, "Large", "Small") End Function
Courtesy: Microsoft Access Help
Let us expand the above function to check a series of expressions to return one of the values among them.
Function CheckIt(TestMe As Integer) CheckIt = IIF(TestMe > 0 AND TestMe < 256, "Byte",IIF(TestMe > 255 AND TestMe < 32768,"Integer","Large")) End Function
As you can see from the above example that for testing of each expression we have to nest the IIF() function one within another, when two or more expressions are evaluated, and it is absolutely necessary that the parentheses are pared properly. This is where we face problems while using this function in Query columns or in criteria rows etc.
If we need a valid value to be returned when none of the expressions evaluated to True (in the above example the text "Large") then IIF() is the correct solution, otherwise we have alternative function Switch().
The Switch() Function is very simple to use without nesting of expressions like IIF(). Let us rewrite the CheckIt() Function with Switch() function to see how simple it is.
Function CheckIt(TestMe As Integer)
CheckIt = Switch(TestMe > 0 AND TestMe < 256, "Byte",TestMe > 255 AND TestMe < 32768,"Integer")
The above function does the same work but if none of the expressions evaluated to True then the result returned in CheckIt variable is Null instead of the text “Large”. If you want to check the returned result for Null and replace Null with the text “Large” then rewrite the expression as below:
CheckIt = NZ(Switch(TestMe > 0 AND TestMe < 256, "Byte",TestMe > 255 AND TestMe < 32768,"Integer"),"Large")
x = Switch(TestMe > 0 AND TestMe < 256, "Byte",TestMe > 255 AND TestMe < 32768,"Integer")
CheckIt = Nz(x,”Large”)
If you are using the Switch() Function in a Query column or criteria row then the first example must be used with Switch() enveloped in Nz().
I think it is easier and compact to use Switch() Function when compared with IIF() that needs repeating the function name IIF with balancing of several parenthesis when several expressions are evaluated.
Usage in a sample Query:
UPDATE Employees SET Employees.Designation = Switch(Employees.Designation IS NULL,'UNKNOWN') WHERE ((Employees.FirstName IS NOT NULL));
See the Switch() Function definition is given below for details, taken from Microsoft Access Help Document.
Switch Function Evaluates a list of expressions and returns a Variant value or an expression associated with the first expression in the list that is True.
Switch(expr-1, value-1[, expr-2, value-2 … [, expr-n,value-n]])
The Switch function syntax has these parts:
Part Description expr Required. Variant expression you want to evaluate. value Required. Value or expression to be returned if the corresponding expression is True.
The Switch function argument list consists of pairs of expressions and values. The expressions are evaluated from left to right, and the value associated with the first expression to evaluate to True is returned. If the parts aren’t properly paired, a run-time error occurs. For example, if expr-1 is True, Switch returns value-1. If expr-1 is False, but expr-2 is True, Switch returns value-2, and so on.
Switch returns a Null value if: None of the expressions is True. The first True expression has a corresponding value that is Null. Switch evaluates all of the expressions, even though it returns only one of them. For this reason, you should watch for undesirable side effects. For example, if the evaluation of any expression results in a division by zero error, an error occurs.
This example uses the Switch function to return the name of a language that matches the name of a city.Function MatchUp (CityName As String) Matchup = Switch(CityName = "London", "English", CityName _ = "Rome", "Italian", CityName = "Paris", "French") End Function