totn Access Functions

MS Access: IF-THEN-ELSE Statement

This MSAccess tutorial explains how to use the Access IF-THEN-ELSE statement with syntax and examples.

Description

The Microsoft Access IF-THEN-ELSE statement can only be used in VBA code.

Syntax

The syntax for the IF-THEN-ELSE statement in MS Access is:

If condition_1 Then
   result_1

ElseIf condition_2 Then
   result_2

...

ElseIf condition_n Then
   result_n

Else
   result_else

End If

Parameters or Arguments

condition_1, condition_2, ... condition_n
Evaluated in the order listed. Once a condition is found to be true, the IF-THEN-ELSE statement will execute the corresponding code and not evaluate the conditions any further.
result_1, result_2, ... result_n
Code that is executed once a condition is found to be true.

Returns

The IF-THEN-ELSE statement evaluates the conditions in the order listed. It will execute the corresponding code when a condition is found to be true.
If no condition is met, then the Else portion of the IF-THEN-ELSE statement will be executed.

Note

  • The ElseIf and Else clauses are optional.

Applies To

The IF-THEN-ELSE function can be used in the following versions of Microsoft Access:

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

Example in VBA Code

The IF-THEN-ELSE statement can only be used in VBA code in Microsoft Access.

First, let's look at a simple example.

If [Region] ="N" Then
   [RegionName] = "North"
End If

Next, let's look at an example that uses ElseIf.

If [Region] ="N" Then
   [RegionName] = "North"

ElseIf [Region] = "S" Then
   [RegionName] = "South"

ElseIf [Region] = "E" Then
   [RegionName] = "East"

ElseIf [Region] = "W" Then
   [RegionName] = "West"

End If

Finally, let's look at an example that uses Else.

If [Region] ="N" Then
   [RegionName] = "North"

ElseIf [Region] = "S" Then
   [RegionName] = "South"

ElseIf [Region] = "E" Then
   [RegionName] = "East"

Else
   [RegionName] = "West"

End If