Format a Text Box for Zip Code

P

PosseJohn

I have a BeforeUpdate event that requires the input to a textbox to be either
5 or 9 digits.

When I set the format for the textbox to "@@@@@-@@@@" and the input is only
5 digits, it shows 1-2345 as the entry.

How can I format the textbox to show 12345 or 12345-6789 (including the dash
if 9 digits).
 
J

Jeff Boyce

When you say "digits", it makes me wonder ... what data type do you have
this data defined as? If "number", stop now! A Zip Code is a code, not a
number. In fact, in places other than the USA, Postal Codes include alpha
characters.

First confirm the data type...

--

Regards

Jeff Boyce
Microsoft Access MVP

Disclaimer: This author may have received products and services mentioned in
this post. Mention and/or description of a product or service herein does
not constitute endorsement thereof.

Any code or pseudocode included in this post is offered "as is", with no
guarantee as to suitability.

You can thank the FTC of the USA for making this disclaimer
possible/necessary.
 
M

Marshall Barton

PosseJohn said:
I have a BeforeUpdate event that requires the input to a textbox to be either
5 or 9 digits.

When I set the format for the textbox to "@@@@@-@@@@" and the input is only
5 digits, it shows 1-2345 as the entry.

How can I format the textbox to show 12345 or 12345-6789 (including the dash
if 9 digits).

When you need to have two different formats, you can use
separate text boxes for user data entry and formatting the
display value. This can be done by adding a text box (named
txtDisplayZip) with the same size and position as your user
entry zip text box. Use the Format - Bring To Front menu
item to make sure the display text box is on top of the
entry text box

Add a line of code to the display text box's GotFocus event
to shift the focus to the entry text box:
Me.yourtextbox.SetFocus

In your entry text box's AfterUpdate event, set the display
text box's value to the formatted value:
If Len(Me.yourtextbox) = 5 Then
txtDisplayZip = Format(Me.yourtextbox, "@@@@@")
Else
txtDisplayZip = Format(Me.yourtextbox, "@@@@@-@@@@")
End If
 
P

PosseJohn

Thank you Marshall, would have tried your suggestion, but Tom had a slightly
better resolution. Happy New Year.
 
P

PosseJohn

Jeff, thankyou for your response, Tom below had a good solution for me. Yes,
the control only accepts numeric, but is actually a text field. I restrict
input using the keydown event. Happy New Year.
 
T

Tony Toews [MVP]

PosseJohn said:
I have a BeforeUpdate event that requires the input to a textbox to be either
5 or 9 digits.

And you'll never, ever have an address in Canada or the rest of the
world?

Tony
--
Tony Toews, Microsoft Access MVP
Tony's Main MS Access pages - http://www.granite.ab.ca/accsmstr.htm
Tony's Microsoft Access Blog - http://msmvps.com/blogs/access/
For a convenient utility to keep your users FEs and other files
updated see http://www.autofeupdater.com/
Granite Fleet Manager http://www.granitefleet.com/
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

Formatting Zip Codes 3
Format US Zip 1
Format Zip Code 2
insert a dash 5
Access 2 Digit year in Access Text Box 3
Zip codes 1
formatting zip codes in excel 1
Testing for numeric input 4

Top