Microsoft Office 2000/Visual Basic Programmer's Guide   

Overriding the Default String-Comparison Setting

To perform a string comparison within a procedure and override the string-comparison setting for the module, you can use the StrComp function. The StrComp function takes two strings as arguments, along with a compare argument, which you can use to specify the type of comparison. The possible settings for the compare argument are vbBinaryCompare, vbTextCompare, and (in Access) vbDatabaseCompare. If you omit this argument, the StrComp function uses the module's default comparison method.

The following table lists the possible return values for the StrComp function.

If Then StrComp returns
string1 < string2 -1
string1 = string2 0
string1 > string2 1
string1 Or string2 Is Null Null

For example, running the following code from the Immediate window prints "1", indicating that the ANSI value of the first character in the first string is greater than the ANSI value of the first character in the second string:

? StrComp("vba", "VBA", vbBinaryCompare)

On the other hand, if you specify text-based string comparison, this code prints "0", indicating that the two strings are identical:

? StrComp("vba", "VBA", vbTextCompare)

Other VBA string functions that perform string comparison also provide a compare argument that you can use to override the default string-comparison setting for that function call. For example, the InStr and InStrRev functions both have a compare argument.