Notice: This website is an unofficial Microsoft Knowledge Base (hereinafter KB) archive and is intended to provide a reliable access to deleted content from Microsoft KB. All KB articles are owned by Microsoft Corporation. Read full disclaimer for more details.

ACC2000: Flexible Input Mask for Entering Five- or Nine-Digit ZIP Codes


View products that this article applies to.

This article was previously published under Q208949
Moderate: Requires basic macro, coding, and interoperability skills.

↑ Back to the top


Summary

This article shows you how to create a flexible input mask for entering either 5- or 9-digit ZIP codes in a field on a form.

Microsoft provides programming examples for illustration only, without warranty either expressed or implied. This includes, but is not limited to, the implied warranties of merchantability or fitness for a particular purpose. This article assumes that you are familiar with the programming language that is being demonstrated and with the tools that are used to create and to debug procedures. Microsoft support engineers can help explain the functionality of a particular procedure, but they will not modify these examples to provide added functionality or construct procedures to meet your specific requirements.

↑ Back to the top


More information

The method demonstrated in this article uses three user-defined sample Visual Basic for Applications functions that do the following:
  • Convert the ZIP code into a text string.
  • Strip the text string of any parentheses, spaces, or dashes.
  • Determine whether the ZIP code contains any alphabetical characters, and if so, terminate the function and alert the user.
To set up the input mask, follow these steps:
  1. Create a new module and enter the following line in the Declarations section if it is not already there:
    Option Explicit
    					
  2. Enter the following code in the module:
    Function Zip (zipcode As Control)
    
      'Exit if a null is passed to the function.
      If IsNull(zipcode) Then
        Exit Function
      End If
      If IsThereAlpha(zipcode) Then
        Msgbox "Your ZIP Code Contains Letters"
        Exit Function
      Else
        'Strip unwanted characters, leaving only numbers.
        zipcode = ZStrip(zipcode, "-")
        zipcode = ZStrip(zipcode, " ")
        zipcode = ZStrip(zipcode, ")")
        zipcode = ZStrip(zipcode, "(")
    
        'Reformat the character string.
        Select Case Len(zipcode)
          Case 5
            Screen.ActiveControl = Format(zipcode, "@@@@@")
          Case 9
            Screen.ActiveControl = Format(zipcode, "@@@@@-@@@@")
          Case Else
            MsgBox "This is not a valid ZIP Code."
            Screen.ActiveControl = zipcode
        End Select
      End If
    End Function
    
    Function ZStrip (InZip, StripZip)
      Do While InStr(InZip, StripZip)
        InZip = Mid(InZip, 1, InStr(InZip, StripZip) - 1) & Mid _
        (InZip, InStr(InZip, StripZip) + 1)
      Loop
      ZStrip = InZip
    End Function
    
    Function IsThereAlpha (zipcode) As Integer
       Dim Pos, a_char$, Clean
       Pos = 1
       Clean = 0
       While (Pos <= Len(zipcode) And (Clean = 0))
          a_char$ = Mid(zipcode, Pos, 1)
          If a_char$ >= "0" And a_char$ <= "9" Then
             Clean = 0
          Else
             If a_char$ <> "-" Then Clean = 1
          End If
          Pos = Pos + 1
       Wend
       IsThereAlpha = Clean
    End Function
    
    					
  3. Type the following in the ValidationRule property of the control that contains the ZIP code:

    Len([Control name]) = 5 or Len([Control name]) = 9
  4. Type the following for the validation text for the control:

    This is not a valid ZIP code. ZIP codes must be 5 or 9 characters in length. Please re-enter the correct ZIP code.
  5. Set the AfterUpdate property of the control on your form that contains the ZIP codes to the expression =Zip([<controlname>]) where <controlname> is the name of the control that contains the ZIP codes.

↑ Back to the top


References

For more information about input masks, click Microsoft Access Help on the Help menu, type input masks in the Office Assistant or the Answer Wizard, and then click Search to view the topics returned.

↑ Back to the top


Keywords: KB208949, kbprogramming, kbinfo

↑ Back to the top

Article Info
Article ID : 208949
Revision : 5
Created on : 10/11/2006
Published on : 10/11/2006
Exists online : False
Views : 261