Option Compare

Returns the result of a string comparison (Integer).
If you omit the optional compare argument then the module's default comparison setting will be used.
A much safer way to compare strings rather than relying on module level statements and comparison operators is to use this function.
This function will override any module level string comparison settings.
StrComp
However if you do no explicitly define the VBA.vbCompareMethod in this function it will use the compare method defined at the top of the module.

Option CompareVBA.vbCompareMethod  
BinaryvbBinaryCompare0Default Case Sensitive - based on a sort order derived from their internal binary representation.
Performs a binary string comparison (A < a)
TextvbTextCompare1Case-Insensitive - based on order in the ASCII table
Performs a textual string comparison (A = a)
DatabasevbDatabaseCompare2Case-Insensitive - This can only be used within Microsoft Access.
Performs a comparison based on information contained within a Microsoft Access database

If a module does not include an Option Compare statement at the top then Binary comparison is used.


Option Compare Binary - Case Sensitive

Option Compare Binary 

This means that the individual characters are compared meaning that uppercase characters are always less than lowercase characters.
A < B < a < b
This compares strings based on a sort order derived from their internal binary representation of each character.

StrComp(string1, string2 [,compare]) 
StrComp("vba", "VBA", vbBinaryCompare) = 1
StrComp("vba", "VBA", vbTextCompare) = 0

StrComp("A", "B") = -1
StrComp("A", "A") = 0
StrComp("B", "A") = 1
StrComp("A", "B", vbBinaryCompare) = -1
StrComp("A", "A", vbBinaryCompare) = 0
StrComp("B", "A", vbBinaryCompare) = 1

StrComp("a", "B") = -1
StrComp("a", "A") = 0
StrComp("b", "A") = 1
StrComp("a", "B", vbBinaryCompare) = -1
StrComp("a", "A", vbBinaryCompare) = 0
StrComp("b", "A", vbBinaryCompare) = 1

StrComp("5", "5", vbBinaryCompare) = 0
StrComp("10", "5", vbBinaryCompare) = -1
StrComp("5", "10", vbBinaryCompare) = 1
StrComp("TEXT", "text", vbBinaryCompare) = -1

StrComp("3 A", "3- B", vbBinaryCompare) = -1
StrComp("3 C", "3- B", vbBinaryCompare) = -1


Option Compare Text - Not Case Sensitive

Option Compare Text 

This compares strings based on case-insensitive text sort order
Never use this
These two text strings are identical but are in different cases

StrComp("TEXT", "text", vbTextCompare) = 0 


"3 A" < "3- B" < "3 C"

StrComp("3 A", "3- B", vbTextCompare) = -1 
StrComp("3 C", "3- B", vbTextCompare) = 1


Option Compare Database

Only available for Access databases. For new modules this is added automatically.

Option Compare Database 

This can only be used within Microsoft Access.
The default is case-insensitive.
This compares strings based on the sort order determined by the local ID of the database whether the string comparison occurs.
This is the default string comparison setting but if this is removed from the top of the module, then Option Compare Binary will be used.


© 2017 Better Solutions Limited. All Rights Reserved. © 2017 Better Solutions Limited

PrevNext