StrComp Function

See Also1AG3TAY                 Example4IPVLV>Low

Returns a Variant8PHEAW3 that indicates the result of the comparison0QKI7U of two string arguments.


StrComp(stringexpr1, stringexpr2 [, compare] )


The StrComp function has these parts:

Part                 Description


stringexpr1       Any string expression1330R89.

stringexpr2       Any string expression.

compare           Specifies the string-comparison0QKI7U method.  The argument compare can be omitted; it can be 0 or 1; or it can be the value of the CollatingOrder property of a Field object.  If compare is omitted, the module default string-comparison method is used.  Unless you use Option Compare Text to change the default comparison method, the default is Option Compare Binary.

                        If compare is 0, string comparison is case-sensitive; so, for example, "M" is less than "m".  If compare is not 0, string comparison is not case-sensitive; so, for example, "M" matches "m".

                        If you want to sort or compare values from a Database, Snapshot or Dynaset in the same way the Database would, you must provide the CollatingOrder property of the Field object.


The StrComp function returns a Variant that indicates the relationship between the string expressions.  Regardless of underlying data type3GYXY7 (Variant or String), each string expression argument is converted to a Variant and then both arguments are compared.  The following table illustrates the possible StrComp return values:

Return value    Condition


1 stringexpr1 < stringexpr2

0                      stringexpr1 = stringexpr2

1                      stringexpr1 > stringexpr2

Null                  stringexpr1 = Null or stringexpr2 = Null

See Also

Instr FunctionPSLW56

Option Compare Statement1EF8XDE

StrComp Function Example

The example uses StrComp to compare two variables.  If the default comparison method is in effect (Option Compare Binary), this code will indicate that Var1 is less than Var2.  However, if Option Compare Text is in effect, the comparison will indicate that the variables are equal.  To try this example, paste the code into the Declarations section of a form.  Then press F5 and click the form.

Sub Form_Click ()
  Dim Cmp, Msg, Var1, Var2               ' Declare variables.
  Var1 = "ABCD": Var2 = "abcd"           ' Define variables.
  Cmp = StrComp(Var1, Var2)              ' Compare variables.
  Select Case Cmp
    Case -1
      Msg = "Var1 is less than Var2."
    Case 0
      Msg = "Var1 is equal to Var2."
    Case 1
      Msg = "Var1 is greater than Var2."
  End Select
  MsgBox Msg                             ' Display message.
End Sub