2016-02-19 78 views
2

我在窗體中有多個組合框(acct_nbr,type,team_aud)。我正在尋找一種基於每個組合框的選擇過濾子表單(作爲數據表)的方法。如果組合框未在過濾器中使用,則子窗體數據僅在另外兩個組合框上進行過濾。如何過濾表單中帶有多個組合框的Access子表單?

這是我到目前爲止有:

Private Sub cboAccountFilter_AfterUpdate() 
    Call FilterSubform 
End Sub 

Private Sub cboTypeFilter_AfterUpdate() 
    Call FilterSubform 
End Sub 

Private Sub txtTeamAuditorFilter_AfterUpdate() 
    Call FilterSubform 
End Sub 

Private Sub FilterSubform() 
    Dim strWhere As String 

    If Nz(Me.cboAccountFilter, "") <> "" Then 
     strWhere = strWhere & "[acct_nbr] = '" & Me.cboAccountFilter & " ' AND " 
    End If 

    If Nz(Me.cboTypeFilter, "") <> "" Then 
     strWhere = strWhere & "[Type] = '" & Me.cboTypeFilter & " ' AND " 
    End If 

    If Nz(Me.txtTeamAuditorFilter, "") <> "" Then 
     strWhere = strWhere & "[team_aud] = '" & Me.txtTeamAuditorFilter & " ' AND " 
    End If 

    If strWhere <> "" Then 
     strWhere = Left(strWhere, Len(strWhere) - 5) 
     Me.fsubStatsDashPrimarySix.Form.Filter = strWhere 
     Me.fsubStatsDashPrimarySix.Form.FilterOn = True 
    Else 
     Me.fsubStatsDashPrimarySix.Form.Filter = "" 
     Me.fsubStatsDashPrimarySix.Form.FilterOn = False 
    End If 
End Sub 

當我點擊的組合框一個我沒有收到錯誤,但所有的數據被過濾掉。

+0

請正確格式化源代碼。 –

+1

當您在每個單引號之前消除空格時會發生什麼? - >'「'AND''(即將它們更改爲'」'AND「'」) – HansUp

+0

當您將'strWhere'字符串分配給'Filter'時,您應該明智地檢查它。在「立即」窗口中添加'Debug.Print strWhere',運行代碼並查看其輸出。 (Ctrl + g會帶你到那裏。) – HansUp

回答

2

更改您的過濾器,以這樣的:

If Nz(Me.cboAccountFilter, "") <> "" Then 
    strWhere = strWhere & "[acct_nbr] = '" & Trim(Me.cboAccountFilter) & "' AND " 
End If 

If Nz(Me.cboTypeFilter, "") <> "" Then 
    strWhere = strWhere & "[Type] = '" & Trim(Me.cboTypeFilter) & "' AND " 
End If 

If Nz(Me.txtTeamAuditorFilter, "") <> "" Then 
    strWhere = strWhere & "[team_aud] = '" & Trim(Me.txtTeamAuditorFilter) & "' AND " 
End If 

編輯:

按您的意見,我認爲這將是:

strWhere = strWhere & "[team_aud] LIKE *'" & Trim(Me.txtTeamAuditorFilter) & "'* AND " 

(我試圖離開,作爲一個評論,但由於SO的標記語言,星號被解釋爲斜體)。

+0

這很好用!謝謝! 我將如何將txtTeamAuditorFilter更改爲包含而不是等於? – ugadawgs306

+0

@ ugadawgs306 - 查看我的編輯。 –

相關問題