Home Privacy Policy Feedback Link to us Site Map Forums

Access: Switch Function


In Access, the Switch function evaluates a list of expressions and returns the corresponding value for the first expression in the list that is TRUE.

The syntax for the Switch function is:

Switch ( expression1, value1, expression2, value2, ... expression_n, value_n )

expression1, expression2, expression_n is a list of expressions that are evaluated. The Switch function is looking for the first expression that evaluates to TRUE.

value1, value2, ... value_n is a list of values. The Switch function will return the value associated with the first expression that evaluates to TRUE.


For example:

Switch (SupplierID=1, "IBM", SupplierID=2, "HP", SupplierID=3, "Nvidia")

In this example, if the SupplierID field is 1, then the Switch function will return "IBM".
If the SupplierID field is 2, then the Switch function will return "HP".
If the SupplierID field is 3, then the Switch function will return "Nvidia".

VBA Code

The Switch function can be used in VBA code. For example:

Dim LValue As String

LValue = Switch (SupplierID=1, "IBM", SupplierID=2, "HP", SupplierID=3, "Nvidia")


SQL/Queries

You can also use the Switch function in a query.

In this example, if the SupplierID field is 1, then the Switch function will return "IBM".
If the SupplierID field is 2, then the Switch function will return "HP".
If the SupplierID field is 3, then the Switch function will return "Nvidia".