Used at module level to declare the default comparison method to use when string data is compared.
Option Compare {Binary | Text | Database}
If used, the Option Compare statement must appear in a module before any statements that declare variables or define constants.
The Option Compare statement specifies the string comparison method (Binary, Text, or Database) for a module. If a module doesn’t include an Option Compare statement, the default text comparison method is Binary.
Option Compare Binary results in string comparisons based on a sort order derived from the internal binary representations of the characters. Sort order is determined by the code page. In the following example, a typical binary sort order is shown:
A < B < E < Z < a < b < e < z < À < Ê < Ø < à < ê < ø
Option Compare Text results in string comparisons based on a case-insensitive text sort order determined by your system’s locale. The same characters shown above, when sorted using Option Compare Text, produce the following text sort order:
(A=a) < ( À=à) < (B=b) < (E=e) < (Ê=ê) < (Z=z) < (Ø=ø)
Option Compare Database can only be used within Microsoft Access. This results in string comparisons based on the sort order that is determined by the locale ID of the database where the string comparisons occur.
InStr Function, Option Base Statement, Option Explicit Statement, Option Private Statement, StrComp Function.
In Microsoft Access, all modules by default contain the Option Compare Database statement in the Declarations section. This statement sets the string comparison method for the module to the one specified for the entire database.
You can change the string comparison method for all new databases by clicking Options on the Tools menu and changing the value in the New Database Sort Order box on the General tab of the Options dialog box. By default this value is set to General, which specifies a case-insensitive sort order based on the English alphabet.
Changing the New Database Sort Order option doesn’t affect the string comparison method for the current database. It only affects databases that are created after the option has been set.
To specify a string comparison method for an individual module that is different from the one specified for the database, change Option Compare Database to Option Compare Binary or Option Compare Text. The Option Compare Binary statement compares strings by evaluating the ASCII values corresponding to the characters they contain, so the statement is case-sensitive. Option Compare Text is case-insensitive.
Note If you are using the Bookmark property of a Recordset object, you must include an Option Compare Binary statement in the Declarations section of the module. The setting and return value for the Bookmark property is a Variant array of Byte data. If you use a case-insensitive string comparison method, the Bookmark property may point you to the wrong record.
This example uses the Option Compare statement to set the default string comparison method. The Option Compare statement is used at the module level only.
' Set the string comparison method to Binary.Compare Binary ' That is, "AAA" is less than "aaa". ' Set the string comparison method to Text.Compare Text ' That is, "AAA" is equal to "aaa".