【问题标题】:How do I filter an Access subform with multiple combo boxes in the form?如何过滤表单中具有多个组合框的 Access 子表单?
【发布时间】: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


【解决方案1】:

将您的过滤器更改为:

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 的标记语言,星号被解释为斜体)。

【讨论】:

  • 这很棒!谢谢!如何将 txtTeamAuditorFilter 更改为 contains 而不是 equals?
猜你喜欢
  • 1970-01-01
  • 2020-10-31
  • 2017-06-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-10-15
相关资源
最近更新 更多