Tech Info,
Windows 95, 98, ME, XP,
NT4, 2000, Errors,
Search products ,
Search FAQs.
Shop by...

Answer to your Issue:

- TA00492 -

Access 2.0 Switch Function

Evaluates a list of expressions and returns a value or an expression associated with the first expression in the list that is True (-1).


Switch(varexpr1, var1 [, varexpr2, var2 . . . [, varexpr7, var7]] )

The Switch function uses the following arguments.

Argument Description
varexpr Expression you want to evaluate. You can include a maximum of seven varexpr arguments.
var Value or expression that is returned if the corresponding varexpr expression is True

The Switch function evaluates the expressions in the varexpr list and returns the contents of the first 
var argument whose corresponding varexpr expression evaluates True. For example, if varexpr1
is True, Switch returns var1. If varexpr1 is False (0), but varexpr2 is True, Switch returns var2
, and so on.

The following example returns the name Speedy, United, or Federal, depending on the value in the 
Ship Via field.

=Switch([Ship Via] = 1, "Speedy", [Ship Via] = 2, "United", [Ship Via] = 3, "Federal")

Switch returns a Null if:

None of the varexpr expressions is True.
The leftmost varexpr that is True has a corresponding var 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 varexpr results in a 
Division by zero error, an error occurs.
Access Basic doesn't check the pairing of varexpr and var arguments at design time. If the 
arguments aren't properly paired, a run-time error occurs.

Switch Function Example

This example assumes that you have created a table named Lang, which contains country names and languages. Switch evaluates the Ship 
Country and Ship City fields to determine the appropriate language for a specified city. The expression is shown on multiple lines for clarity; you could also enter it on a single line.

=Switch([Ship City] = "Melbourne", "English",
[Ship City] = "Lucerne", "German",
[Ship City] = "Rome", "Italian",
[Ship Country] = "CH", "French",
True, "English")

If the city is Melbourne, Switch returns "English"; if it is Lucerne, it returns "German"; and so on. If the city is not one of those listed, but the country is Switzerland ("CH" is the abbreviation for Switzerland), it returns "French". Otherwise, Switch returns "English".



Contact Us, Specials