我在窗体中有多个组合框(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
当我点击的组合框一个我没有收到错误,但所有的数据被过滤掉。
请正确格式化源代码。 –
当您在每个单引号之前消除空格时会发生什么? - >'“'AND''(即将它们更改为'”'AND“'”) – HansUp
当您将'strWhere'字符串分配给'Filter'时,您应该明智地检查它。在“立即”窗口中添加'Debug.Print strWhere',运行代码并查看其输出。 (Ctrl + g会带你到那里。) – HansUp