StrComp Function

Returns a Variant (Integer) indicating the result of a string expression (string expression: An expression that evaluates to a sequence of contiguous characters. Elements of the expression can be: functions that return a string or a string Variant (VarType 8); a string literal, constant, variable, or Variant.).


StrComp(string1, string2 [, compare ] )

The StrComp function syntax has these arguments (argument: A value that provides information to an action, an event, a method, a property, a function, or a procedure.):

Argument Description
string1 Required. Any valid string expression.
string2 Required. Any valid string expression.
compare Optional. Specifies the type of string comparison. If the compare argument (argument: A value that provides information to an action, an event, a method, a property, a function, or a procedure.) is Null (Null: A value you can enter in a field or use in expressions or queries to indicate missing or unknown data. In Visual Basic, the Null keyword indicates a Null value. Some fields, such as primary key fields, can't contain a Null value.), an error occurs. If compare is omitted, the Option Compare setting determines the type of comparison.


The compare argument settings are:

Constant Value Description
vbUseCompareOption -1 Performs a comparison using the setting of the Option Compare statement.
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


 Note   Examples that follow demonstrate the use of this function in a Visual Basic for Applications (VBA) module. For more information about working with VBA, select Developer Reference in the drop-down list next to Search and enter one or more terms in the search box.

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 = MyStr1 MyStr2 1    ' Returns 0.
MyComp = MyStr1 MyStr2 0    ' Returns -1.
MyComp = MyStr2 MyStr1    ' Returns 1.

Applies to:
Access 2007