Share this page:

MS Access: Switch Function

Learn how to use the Access Switch function with syntax and examples.

Description

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

Syntax

The syntax for the Microsoft Access Switch function is:

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

Parameters or Arguments

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.

Applies To

The Switch function can be used in the following versions of Microsoft Access:

  • Access 2013, Access 2010, Access 2007, Access 2003, Access XP, Access 2000

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".

Example (in VBA Code)

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

Dim LValue As String

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

Example (in SQL/Queries)

You can also use the Switch function in a query in Microsoft Access.

Microsoft Access

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".