CompareMode Method

Sets and returns the comparison mode for comparing string keys in a Dictionary object.

Syntax

object.CompareMode[ = compare]

Arguments:

  • Object: Required. Always the name of a Dictionary object.

  • Compare: Optional. If provided, compare is a value representing the comparison mode. Acceptable values are 0 (Binary), 1 (Text), 2 (Database). Values greater than 2 can be used to refer to comparisons using specific Locale IDs (LCID).

Remarks

An error occurs if you try to change the comparison mode of a Dictionary object that already contains data.

The following example illustrates the use of the CompareMode property:

Example 1:

Sub TextCompareExample
    Dim d   ' Create a variable.
    Set d = CreateObject("Scripting.Dictionary")

    ' Set comparison mode to Text (case-insensitive)
    d.CompareMode = vbTextCompare

    ' Add some keys and items.
    d.Add "a", "Athens"
    d.Add "b", "Belgrade"
    d.Add "c", "Cairo"

    ' Try to add a key that differs only in case
    On Error Resume Next
    d.Add "B", "Baltimore"
    Number = Err.Number
    If Number <> 0 Then
              Msg = "Error: Key 'b' already exists."
        Err.Clear
    End If
    On Error GoTo 0
 End Sub
' Output: Error: Key 'b' already exists.

Example 2:

Sub BinaryCompareExample
    Dim d   ' Create a variable.
    Set d = CreateObject("Scripting.Dictionary")

    ' Set comparison mode to Binary (case-sensitive)
    d.CompareMode = vbBinaryCompare

    ' Add some keys and items.
    d.Add "x", "Xenon"
    d.Add "y", "Yttrium"
    d.Add "z", "Zirconium"

    ' Try to add a key "X" which is different from "x"
    d.Add "X", "X-Ray"
 End Sub
' Output: Dictionary Object Contain Keys: x,y,z,X Items: Xenon,Yttrium,Zirconium,X-Ray