【发布时间】:2016-02-19 17:27:45
【问题描述】:
我的表单中有多个组合框(acct_nbr、type、team_aud)。我正在寻找一种基于每个组合框的选择来过滤子表单(作为数据表)的方法。如果过滤器中未使用组合框,则子表单数据仅在其他两个组合框上过滤。
这是我目前所拥有的:
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 ") -
当您将其分配给
Filter时,检查strWhere字符串是否存在也是明智之举。添加Debug.Print strWhere,运行代码,并在“立即”窗口中查看其输出。 (Ctrl+g 会带你去那里。)
标签: ms-access vba ms-access-2013