View contents of this chapter

Rules of Thumb

You can use the conditional operators to compare strings just as you do for numeric values. Generally, string comparisions follow these rules of thumb:

  • Uppercase letters are less than lowercase letters, so "ABC" comes before "abc"
  • Letters compare in alphabetical order, so "A" is less than "B" and the name "Walter" is greater than (comes before) "Williams".
  • Numbers are less than letters, so "5" is less than "Five".
Play Sound VB supports a special statement in the declaration section of a module that reads as follows: Option Compare Text
If this statement appears in a modules declarations section, uppercase and lowercase letters compare equally.

VB supports another conditional operator, Like, which compares values based on a wildcard match. This operator uses * for zero or more characters, ? for only one character, # for a numeric digit. Let's consider some examples:

"Universal Teacher Publications" Like "Univ*"
"Universal" Like "Univers??"
"ABC50" Like "ABC##"

All the above examples return the value 'True'.

"V" Like "[VBA]"

The above example shows a special kind of Like expression. If the character is like any character inside brackets, a 'True' is returned.

Play Sound The Equality conditional operator would return false for these expressions because = does not recognize wildcard characters.

Always compare values whose data types are compatible. Don't try to compare a string and a number, because the results are usually wrong.



© Universal Teacher Publications        INDEX Previous Screen Next Screen