Macro

T

Té

I have a table that contains Cities and States along with their zip codes.
How can I set up a macro where I enter a zip code in a form it automatically
puts in the cities and states?
 
B

bhicks11 via AccessMonster.com

Here's some code I use to do just that. I have a table with the zipcodes,
state and city. The Dlookup is looking in that table for the zipcode that
equals Zip (the current control).

Bonnie
http://www.dataplus-svc.com

Private Sub ZIP_Exit(Cancel As Integer)

Dim varState, varCity As Variant

varState = DLookup("State", "ZipCode", "ZipCode=LEFT([Zip],5)")
varCity = DLookup("City", "ZipCode", "ZipCode=LEFT([Zip],5)")

End Sub
 
S

Steve Schapel

Té,

If you are talking about the City and State being written, via bound
controls on the form, into fields in the table that the form is based
on, then the answer is that this is basically an invalide thing to be
doing in a relational database. You already have the master table with
all the zip codes and their associated cities/states, and this is the
*only* place this data should be stored in your database.

If, on the other hand, you are talking about the *display*, from the
user-friendly point of view, of the city/state information related to
the zip code entered, then this is most understandable, and a common
requirement. There are a number of approaches that can be taken in this
situation. There is a discussion of the options in this article:
http://accesstips.datamanagementsolutions.biz/lookup.htm

I think in the scenario you have mentioned, I would prefer the first
method 'Query' in that article, which would involve adding the
zip/city/state table to the query that the form is based on, joined to
the existing table on the Zip field, which gives you direct access to
the related city and state onto the form.
 
B

bhicks11 via AccessMonster.com

Hi Steve,

We add to the table and review on data entry for validation purposes.
Sometimes there may be a difference on the original document than what the
USPS indicates in which case we would change to the original document as our
job requires.

Bonnie
http://www dataplus-svc.com
 
T

Té

Hi Bonnie,
So do I copy that code into the on exit on my form? I'm new to Access,
and I am just learning about macros.
 
B

bhicks11 via AccessMonster.com

No Te, I put it in the OnExit event of the zipcode field (which I place on
the form just before the city/state) and it files the city, state.

Bonnie
http://www.dataplus
 

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

Top