I'm trying to write a procedure that will allow a user to input his zipcode and the City and State will automatically be updated.
I'm using unbound text to trigger the event procedure. Here is the code:
Private Sub ZipCode_AfterUpdate()
'This Sub looks up a zipcode in the table named
'ZipCodes and, if it finds a matching zipcode, fills in
'the City and State controls from data
'in the ZipCodes table.
'Declare DAO object variables and string variable.
Dim ThisDB As Database
Dim ZipCodes As Recordset
Dim LookFor As String
'Define DAO object variables
Set ThisDB = CurrentDb()
Set ZipCodes = ThisDB.OpenRecordset("ZipCodes"
'Isolate the first five characters in the ZipCode field.
LookFor = Left([ZipCode], 5)
'Define the index to search, then seek the LookFor value
ZipCodes.Index = "PrimaryKey"
ZipCodes.Seek "=", LookFor
'If not found, beep and move focus to the City control
If ZipCodes.NoMatch Then
DoCmd.Beep
DoCmd.GoToControl "City"
Else
'If a matching zipcode is found, fill City and State
'fields then move to the CompanyName control.
[City] = ZipCodes!City
[State] = ZipCodes!State
DoCmd.GoToControl "CompanyName"
End If
End Sub
I'm using unbound text to trigger the event procedure. Here is the code:
Private Sub ZipCode_AfterUpdate()
'This Sub looks up a zipcode in the table named
'ZipCodes and, if it finds a matching zipcode, fills in
'the City and State controls from data
'in the ZipCodes table.
'Declare DAO object variables and string variable.
Dim ThisDB As Database
Dim ZipCodes As Recordset
Dim LookFor As String
'Define DAO object variables
Set ThisDB = CurrentDb()
Set ZipCodes = ThisDB.OpenRecordset("ZipCodes"
'Isolate the first five characters in the ZipCode field.
LookFor = Left([ZipCode], 5)
'Define the index to search, then seek the LookFor value
ZipCodes.Index = "PrimaryKey"
ZipCodes.Seek "=", LookFor
'If not found, beep and move focus to the City control
If ZipCodes.NoMatch Then
DoCmd.Beep
DoCmd.GoToControl "City"
Else
'If a matching zipcode is found, fill City and State
'fields then move to the CompanyName control.
[City] = ZipCodes!City
[State] = ZipCodes!State
DoCmd.GoToControl "CompanyName"
End If
End Sub