Article ID: 120377
Article Last Modified on 1/19/2007
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>])
Keywords: kbinfo KB120377