VBA VBA Option Keyword Option Compare {Binary | Text | Database}


Example

Option Compare Binary

Binary comparison makes all checks for string equality within a module/class case sensitive. Technically, with this option, string comparisons are performed using sort order of the binary representations of each character.

A < B < E < Z < a < b < e < z

If no Option Compare is specified in a module, Binary is used by default.

Option Compare Binary

Sub CompareBinary()

    Dim foo As String
    Dim bar As String
    
    '// Case sensitive
    foo = "abc"
    bar = "ABC"
    
    Debug.Print (foo = bar) '// Prints "False"
    
    '// Still differentiates accented characters
    foo = "ábc"
    bar = "abc"
    
    Debug.Print (foo = bar) '// Prints "False"
    
    '// "b" (Chr 98) is greater than "a" (Chr 97)
    foo = "a"
    bar = "b"
    
    Debug.Print (bar > foo) '// Prints "True"
    
    '// "b" (Chr 98) is NOT greater than "á" (Chr 225)
    foo = "á"
    bar = "b"
    
    Debug.Print (bar > foo) '// Prints "False"

End Sub

Option Compare Text

Option Compare Text makes all string comparisons within a module/class use a case insensitive comparison.

(A | a) < (B | b) < (Z | z)

Option Compare Text

Sub CompareText()

    Dim foo As String
    Dim bar As String
    
    '// Case insensitivity
    foo = "abc"
    bar = "ABC"
    
    Debug.Print (foo = bar) '// Prints "True"
    
    '// Still differentiates accented characters
    foo = "ábc"
    bar = "abc"
    
    Debug.Print (foo = bar) '// Prints "False"
    
    '// "b" still comes after "a" or "á"
    foo = "á"
    bar = "b"
    
    Debug.Print (bar > foo) '// Prints "True"

End Sub

Option Compare Database

Option Compare Database is only available within MS Access. It sets the module/class to use the current database settings to determine whether to use Text or Binary mode.

Note: The use of this setting is discouraged unless the module is used for writing custom Access UDFs (User defined functions) that should treat text comparisons in the same manner as SQL queries in that database.