Code:
Option Compare Database
Option Explicit
Private Sub cboStateCode_AfterUpdate()
' Filter the list of Cities based on the selected State.
' Set the City combo box to be limited by the selected State
Me.cboCity.RowSource = "SELECT Cities.StateCode, Cities.City FROM Cities " & _
" WHERE StateCode = " & Nz(Me.cboStateCode) & _
" ORDER BY City"
Me.cboStateCode = Null
FilterCitiesList
End Sub
Private Sub FilterCitiesList()
Dim strRS As String
' Filter the list box appropriately based on the combo box selection(s)
strRS = "SELECT qryCitiesAndStates.City, qryCitiesAndStates.State, qryCitiesAndStates.StateCode FROM qryCitiesAndStates"
' If Not IsNull(Me.cboCity) Then
' strRS = strRS & " WHERE StateCode = " & Me.cboCity
If Not IsNull(Me.cboStateCode) Then
strRS = strRS & " WHERE StateCode = " & Me.cboStateCode & ""
End If
strRS = strRS & " ORDER BY qryCitiesAndStates.State, qryCitiesAndStates.City;"
Me.lstCities.RowSource = strRS
Me.lstCities.Requery
End Sub
Private Sub Form_Load()
' When the form loads, enable/disable the combo boxes. Combo boxes are only enabled if the preceeding combo box has a value.
' EnableControls
' Show all Cities in the list until filters are selected from the State combo box.
FilterCitiesList
End Sub