Sign in with Microsoft
Sign in or create an account.
Hello,
Select a different account.
You have multiple accounts
Choose the account you want to sign in with.

Returns a Variant (Integer) indicating the result of a string expression.

Syntax

StrComp ( string1, string2 [, compare ] )

The StrComp function syntax has these arguments:

Argument

Description

string1

Required. Any valid string expression.

string2

Required. Any valid string expression.

compare

Optional. Specifies the type of string comparison. If the compareargument is Null, an error occurs. If compare is omitted, the Option Compare setting determines the type of comparison.

Settings

The compare argument settings are:

Constant

Value

Description

vbBinaryCompare

0

Performs a binary comparison.

vbTextCompare

1

Performs a textual comparison.

vbDatabaseCompare

2

Microsoft Office Access 2007 only. Performs a comparison based on information in your database.

Return Values

The StrComp function has the following return values:

If

StrComp returns

string1 is less than string2

-1

string1 is equal to string2

0

string1 is greater than string2

1

string1 or string2 is Null

Null

Query example

Expression

Results

SELECT ProductSales.ProductID, StrComp(ProductID,"PRO_XYZ10") AS ComparisonResult FROM ProductSales;

Returns the values from the field "ProductID", compares the values in "ProductID" with "PRO_XYZ1" and returns the results in column ComparisonResult. "ProductID" is equal to "PRO_XYZ10", the StrComp function will return 0. If "ProductID" is less than "PRO_XYZ10", the StrComp function will return -1. If "ProductID" is greater than "PRO_XYZ10", the StrComp function will return 1. If either "ProductID" or "PRO_XYZ10" is NULL, the StrComp function will return NULL.

VBA example

Note: Examples that follow demonstrate the use of this function in a Visual Basic for Applications (VBA) module.

This example uses the StrComp function to return the results of a string comparison. If the third argument is 1, a textual comparison is performed; if the third argument is 0 or omitted, a binary comparison is performed.

Dim MyStr1, MyStr2, MyComp
MyStr1 = "ABCD": MyStr2 = "abcd" ' Define variables.
MyComp = StrComp(MyStr1, MyStr2, 1) ' Returns 0.
MyComp = StrComp(MyStr1, MyStr2, 0) ' Returns -1.
MyComp = StrComp(MyStr2, MyStr1) ' Returns 1.

String functions and how to use them

Need more help?

Want more options?

Explore subscription benefits, browse training courses, learn how to secure your device, and more.

Communities help you ask and answer questions, give feedback, and hear from experts with rich knowledge.

Was this information helpful?

What affected your experience?
By pressing submit, your feedback will be used to improve Microsoft products and services. Your IT admin will be able to collect this data. Privacy Statement.

Thank you for your feedback!

×