is there a way to have a to select the correct zip if there are multiable zip
codes? thanks for your help
As Randy suggests, it's probably easier to select the Zip first.
Here's some code which I use in my applications - it coordinates the
City, State and Zip combo boxes so that when you select any one of
them, the remaining ones are either set to a single value or limited
to the list of valid values. This code would all be connected to the
AfterUpdate events of the respective combos. My table CONtblZip
contains fields Zip, City and State.
Private Sub cboCity_AfterUpdate()
' Comments :
' Parameters: -
' Modified : 01/29/02 by JWV
'
' --------------------------------------------------
On Error GoTo PROC_ERR
Dim rs As DAO.Recordset
Dim db As DAO.Database
Dim iCount As Integer
Dim strSQL As String
' Set default to all zipcodes
Me.cboZip.RowSource = "SELECT Zip FROM CONtblZip ORDER BY Zip;"
' If a city is selected, limit the Zip to those in the selected
' city; if the city has only one zip, just set it to that value
If Not IsNull(cboCity) Then
Set db = CurrentDb
strSQL = "SELECT Zip, State FROM CONtblZip WHERE " & _
"ContblZip.City=" & Chr(34) & cboCity & Chr(34) & _
IIf(IsNull(Me.cboState), " ", " AND CONtblZip.State = '" _
& Me.cboState & "') ORDER BY City;"
Set rs = db.OpenRecordset(strSQL, dbOpenSnapshot, dbReadOnly)
rs.MoveLast
iCount = rs.RecordCount
Select Case iCount
Case 0
' do nothing if this city isn't in the ZIP table
Case 1
' If there's just one city/zip, set zip and state
' to the selected one
Me.cboZip = rs!Zip
Me.cboState = rs!State
Case Else
' If more than one zipcode, limit the combo
' to the selected city's zipcodes
Me.cboZip.RowSource = strSQL
End Select
rs.Close
Set rs = Nothing
Set db = Nothing
End If
PROC_EXIT:
Exit Sub
PROC_ERR:
MsgBox "Error " & Err.Number & " in cboCity_AfterUpdate:" _
& vbCrLf & Err.Description
Resume PROC_EXIT
End Sub
Private Sub cboState_AfterUpdate()
' Comments :
' Parameters: -
' Modified : 01/29/02 by JWV
'
' --------------------------------------------------
On Error GoTo PROC_ERR
If IsNull(cboState) Then
' State has been cleared, set Zip and City combos to all
Me.cboCity.RowSource = "SELECT DISTINCT City, State " _
& "FROM CONtblZip ORDER BY City;"
Me.cboZip.RowSource = "SELECT Zip FROM CONtblZip " _
& "ORDER BY Zip;"
Else
Me.cboCity.RowSource = "SELECT DISTINCT City, State " _
& "FROM CONtblZip WHERE CONtblZip.State = '" & cboState & "' " _
& "ORDER BY City;"
Me.cboZip.RowSource = "SELECT Zip FROM CONtblZip " _
& "WHERE CONtblZip.State = '" & cboState & "' ORDER BY Zip;"
End If
PROC_EXIT:
Exit Sub
PROC_ERR:
MsgBox "Error " & Err.Number & " in cboState_AfterUpdate:" _
& vbCrLf & Err.Description
Resume PROC_EXIT
End Sub
Private Sub cboZip_AfterUpdate()
' Comments :
' Parameters: -
' Modified : 01/29/02 by JWV
'
' --------------------------------------------------
On Error GoTo PROC_ERR
Dim rs As DAO.Recordset
Dim db As DAO.Database
Dim iCount As Integer
Dim strSQL As String
' Set default to all cities
Me.cboCity.RowSource = "SELECT DISTINCT City, State FROM " _
& "CONtblZip ORDER BY City;"
If Not IsNull(cboZip) Then
Set db = CurrentDb
strSQL = "SELECT DISTINCT City, State FROM CONtblZip " _
& "WHERE ContblZip.Zip='" & cboZip & "' ORDER BY City;"
Set rs = db.OpenRecordset(strSQL, dbOpenSnapshot, dbReadOnly)
iCount = rs.RecordCount
Select Case iCount
Case 0
' do nothing
Case 1
Me.cboCity = rs!City
Me.cboState = rs!State
Case Else
Me.cboCity.RowSource = strSQL
Me.cboState = rs!State
End Select
rs.Close
Set rs = Nothing
Set db = Nothing
End If
PROC_EXIT:
Exit Sub
PROC_ERR:
MsgBox "Error " & Err.Number & " in cboZip_AfterUpdate:" _
& vbCrLf & Err.Description
Resume PROC_EXIT
End Sub
Private Sub Form_Current()
' Comments :
' Parameters: -
' Modified : 01/29/02 by JWV
'
' --------------------------------------------------
On Error GoTo PROC_ERR
If IsNull(Me.cboState) Then
Me.cboCity.RowSource = "SELECT DISTINCT City, State " _
& "FROM CONtblZip ORDER BY City;"
Me.cboZip.RowSource = "SELECT Zip " _
& "FROM CONtblZip ORDER BY Zip;"
Else
Me.cboCity.RowSource = "SELECT DISTINCT City, State " _
& "FROM CONtblZip WHERE [State]='" & cboState & "' ORDER BY City;"
Me.cboZip.RowSource = "SELECT Zip FROM CONtblZip " _
& "WHERE [State]='" & cboState & "' ORDER BY Zip;"
End If
PROC_EXIT:
Exit Sub
PROC_ERR:
MsgBox "Error " & Err.Number & " in Form_Current:" _
& vbCrLf & Err.Description
Resume PROC_EXIT
End Sub
John W. Vinson[MVP]
Join the online Access Chats
Tuesday 11am EDT - Thursday 3:30pm EDT
http://community.compuserve.com/msdevapps