totn Access Functions

MS Access: StrComp Function

This MSAccess tutorial explains how to use the Access StrComp function with syntax and examples.

Description

The Microsoft Access StrComp function returns an integer value representing the result of a string comparison.

Syntax

The syntax for the StrComp function in MS Access is:

StrComp ( string1, string2 [, compare ] )

Parameters or Arguments

string1 and string2
The two strings to compare to each other.
compare

Optional. This is the type of comparison to perform. The valid choices are:

VBA Constant Value Explanation
vbUseCompareOption -1 Uses option compare
vbBinaryCompare 0 Binary comparison
vbTextCompare 1 Textual comparison
vbDatabaseCompare 2 Comparison based on your database

Note

  • If string1 is equal to string2, the StrComp function will return 0.
  • If string1 is less than string2, the StrComp function will return -1.
  • If string1 is greater than string2, the StrComp function will return 1.
  • If either string1 or string2 is NULL, the StrComp function will return NULL.

Applies To

The StrComp 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

Let's look at how to use the StrComp function in MS Access:

StrComp ("TechOnTheNet.com", "TechOnTheNet.com")
Result: 0

StrComp ("TechOnTheNet.com", "Abc")
Result: 1

StrComp ("TechOnTheNet.com", "Xyz")
Result: -1

StrComp ("TechOnTheNet.com", NULL)
Result: NULL

Example in VBA Code

The StrComp function can be used in VBA code in Microsoft Access.

For example:

Dim LResult As Integer

LResult = StrComp ("TechOnTheNet.com", "TechOnTheNet.com")

In this example, the variable called LResult would now contain the value 0 since both string values are equal.

Example in SQL/Queries

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

For example:

Microsoft Access

In this query, we have used the StrComp function as follows:

Expr1: StrComp([website_name],"TechOnTheNet.com")

This query will compare the website_name field to the string "TechOnTheNet.com" and display the results in a column called Expr1. You can replace Expr1 with a column name that is more meaningful.

For example:

Comparison: StrComp([website_name],"TechOnTheNet.com")

The results would now be displayed in a column called Comparison.