3 LIKE what?

VBA (and most implementations of BASIC from Microsoft) offers an operator called LIKE. Unlke the equal operator (=), the LIKE operator does not look for an exact match. Instead, it checks to see if a string fits a pattern description.

This makes the LIKE operator quite useful. For example, if PostalCodeText is a text box in a form for entering postal code, the following code can validate it:

If Not (PostalCodeText.Value LIKE "[A-Z]#[A-Z] #[A-Z]#") Then  
  MsgBox("The postal code is invalid")  
  Cancel = True  
End If  
  

Let us explain the first line of this code a little more:

Let us now take a closer look at what can be specified in a pattern.

 3.1 A character in a range
 3.2 A character in a set
 3.3 A character not in a range
 3.4 A character not in a set
 3.5 A single digit
 3.6 Any single character
 3.7 Any number of any characters