简体   繁体   中英

Using multiple Combo-boxes in Access as query criteria dont work together but using one combo-box works ?? How to make all Combo-boxes work?

I looks like when i use one Combo-box as a criteria works just fine however using more than that though following the same steps doesn't work. I don't use SQL i do use the Design view though.

How to make all combo boxes works together to provide the needed criteria.

If you are looking to filter a form or list box using data selected from several combo boxes, then you will need to build up the RowSource "on the fly" based on the selections made.

Below is some sample code that uses selections from 2 combo boxes (cboCountry and cboRMZone) to create the RowSource for a list box (lstCountry):

Private Sub cboCountryZone_AfterUpdate()
    Call sSearchMultiple
End Sub

Private Sub cboRMZone_AfterUpdate()
    Call sSearchMultiple
End Sub

Private Sub Form_Load()
    Call sSearchMultiple
End Sub

Private Sub sSearchMultiple()
    On Error GoTo E_Handle
    Dim strSQL As String
    If Not IsNull(Me!cboCountryZone) Then strSQL = strSQL & " AND CountryZone_PK=" & Me!cboCountryZone
    If Not IsNull(Me!cboRMZone) Then strSQL = strSQL & " AND RMZone_PK=" & Me!cboRMZone
    If Left(strSQL, 4) = " AND" Then
        strSQL = " WHERE " & Mid(strSQL, 6)
    End If
    If Len(strSQL) > 0 Then
        Me!lstCountry.RowSource = "SELECT CountryName FROM dbo_svr_Country " & strSQL & " ORDER BY CountryName ASC;"
    Else
        Me!lstCountry.RowSource = "SELECT CountryName FROM dbo_svr_Country ORDER BY CountryName ASC;"
    End If
sExit:
    On Error Resume Next
    Exit Sub
E_Handle:
    MsgBox Err.Description & vbCrLf & vbCrLf & "Form3!sSearchMultiple", vbOKOnly + vbCritical, "Error: " & Err.Number
    Resume sExit
End Sub

Regards,

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM