StrComp Function
Description
Returns a Variant (Integer) indicating the result of a string comparison.
Syntax
StrComp(string1, string2[, compare])
The StrComp function syntax has these named arguments:
Part | Description |
|
string1 | Required. Any valid string expression. |
string2 | Required. Any valid string expression. |
compare | Optional. Specifies the type of string comparison. The compare argument can be omitted, or it can be 0, 1 or 2. Specify 0 (default) to perform a binary comparison. Specify 1 to perform a textual comparison. For Microsoft Access only, specify 2 to perform a comparison based on information contained in your database. If compare is Null, an error occurs. If compare is omitted, the Option Compare setting determines the type of comparison. |
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 |
See Also
InStr function, Option Compare statement.
Example
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.