I like the kludge method. Here's how I do it:
the ZIP codes are in a table called tblZIPCodes
I am trying to enter a person into a form called frmContacts (recordsource is the table tblContacts), with controls txtZIP, txtCity and txtState.
After entering my ZIP in txtZIP, I trigger a LostFocus event. The LostFocus event opens another form, called frmZIPKludge, that has its recordsource set to the following SQL:
SELECT tblZIPCodes.City, tblZIPCodes.State, tblZIPCodes.ZIP
FROM tblZIPCodes
WHERE (((tblZIPCodes.ZIP)=[Forms]![frmContacts]![txtZIP]));
frmZIPKludge has the controls txtCity, txtState, and txtZIP set to the corresponding table fields.
The code in the LostFocus event looks something like this:
Private Sub txtZIP_LostFocus
Dim HadFocus
HadFocus = Screen.ActiveControl
DoCmd.OpenForm "frmZIPKludge",,,,acHidden
Me.txtCity = Forms!frmZIPKludge!txtCity
Me.txtState = Forms!frmZIPKludge!txtState
DoCmd.Close acForm, "frmZIPKludge"
Me.HadFocus.SetFocus
End Sub
I think that's about right. I was checking to make sure and Access locked up on me.
----------------------------
Oooh, I like your code there... I've never used DLookUp before, but looking at Access VBA help... Yeah, that would work! Sweet, you've rendered my Kludge obsolete! I ♥ U!!
Basically, what you're doing there is setting up variables as containers to hold information pulled up by the DLookup function, which pulls up the specified field in the specified table with the specified criteria. Then, if the ZIP Code existed in your database (signified by the variables not holding the null state, aka not being empty) the information held in those two variables would be put into the City and State fields on your form.
The table holding the ZIPs in this code is tblZipCode, containing field City, State and ZipCode.
the controls on the form are called City, State and Zip
if that's set up right, then you should have no problem with the code.