Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Hello, I try to fill fields auto

Status
Not open for further replies.

Guest_imported

New member
Jan 1, 1970
0
Hello,

I try to fill fields automatically on form based on a control's value.

I have fields that there are "ST(state), CITY, ZIP" in table "MOTOR_CARRIERS". The form should be: When I enter Zip I will never have to enter State/City again.

The following are my codes:(but not work)

Private Sub ZIP_Exit(Cancel As Integer)

Dim varST, varCity As Variant
varST = DLookup("[ST]", "MOTOR_CARRIERS", "[ZIP]=ZIP")
varCity = DLookup("[CITY]", "MOTOR_CARRIERS", "[ZIP]=ZIP")
If (Not IsNull(varST)) Then Me![ST] = varST
If (Not IsNull(varCity)) Then Me![CITY] = varCity

End Sub

Any suggestion?

Many Thanks.
Eric
 
Eric,

Your syntax is wrong.
Private Sub ZIP_Exit(Cancel As Integer)

Dim varST, varCity As Variant
varST = DLookup("[ST]", "MOTOR_CARRIERS", "[ZIP]='" & me.ZIP & "'")
varCity = DLookup("[CITY]", "MOTOR_CARRIERS", "[ZIP]='" & me.ZIP & "'")
If (Not IsNull(varST)) Then Me![ST] = varST
If (Not IsNull(varCity)) Then Me![CITY] = varCity

End Sub

The way you had it originally made Access ask for a City or State value where [ZIP] = "[ZIP]". Since, there is no Zipcode in the US like that, you're not going to get the results you're looking for.

Good Luck.

 
Can this still be done if the fields you want auto-displayed on the form are from different tables and if so what needs to be changed?
Much appreciated.
 
I doesn't matter what table is used to retrieve the values. Just keep the right syntax.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top