This article was previously published under Q208949
Moderate: Requires basic macro, coding, and interoperability skills.
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.
View products that this article applies to.
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
Keywords: KB208949, kbprogramming, kbinfo