ACC: Flexible Input Mask for Entering 5- or 9-Digit ZIP CodesID: Q120377
|
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.
This article assumes that you are familiar with Visual Basic for
Applications and with creating Microsoft Access applications using the
programming tools provided with Microsoft Access. For more information
about Visual Basic for Applications, please refer to your version of the
"Building Applications with Microsoft Access" manual.
NOTE: Visual Basic for Applications is called Access Basic in Microsoft
Access versions 1.x and 2.0. For more information about Access Basic,
please refer to the "Introduction to Programming" manual in Microsoft
Access version 1.x or the "Building Applications" manual in Microsoft
Access version 2.0.
The method demonstrated in this article uses three user-defined sample
Visual Basic (or Access Basic) functions that perform the following tasks:
Option Explicit
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
This is not a valid zip code. Zip codes must be 5 or 9 characters in length. Please re-enter the correct zip code.
=Zip([<controlname>])
For more information about using input masks, search the Help Index for "input masks," and then "Create an input mask to control how data is entered in a field or control."
Keywords : FmsProp
Version : 1.0 1.1 2.0 7.0 97
Platform : WINDOWS
Issue type : kbinfo
Last Reviewed: April 7, 1999