温馨提示:本文翻译自stackoverflow.com,查看原文请点击:vba - MS Access filtering issue
ms-access vba

vba - MS Access筛选问题

发布于 2020-04-08 00:06:31

我在基于组合框自动过滤报表/表单时遇到问题。

此特定行:

Form_sfrmSpending.Filter = strAT & " AND " & strBur & " AND " & strFT

如何修复此语法?

谢谢

Dim strBur As String
Dim strAT As String
Dim strFT As String

strBur = "Bureau='" & Me.cboBur.Value & "'"
strAT = "Categories'" & Me.cboCategories.Value & "'"
strFT = "[Funding Type]'" & Me.cboFunding.Value & "'"


If cboBur = "All Bureaus" Then
If cboCategories = "All Categories" Then
If cboFunding = "All Funding Type" Then
Form_sfrmSpending.Filter = ""
Form_sfrnSpending.FilterOn = False
End If
End If
End If


If cboBur <> "All Bureaus" Then
If cboCategories = "All Categories" Then
If cboFunding = "All Funding Type" Then
Form_sfrmSpending.Filter = strBur
 Form_sfrmSpending.FilterOn = True
End If
End If
End If

If Me.cboCategories <> "All Categories" Then
If cboBur = "All Bureaus" Then
If cboFunding = "All Funding Type" Then
Form_sfrmSpending.Filter = strAT
Form_sfrmSpending.FilterOn = True
End If
End If
End If

If Me.cboFunding <> "All Funding Type" Then
If cboBur = "All Bureaus" Then
If cboCategories = "All Categories" Then
Form_sfrmSpending.Filter = strFT
Form_sfrmSpending.FilterOn = True
End If
End If
End If

If Me.cboCategories <> "All Action Types" Then
If cboBur <> "All Bureaus" Then
If cboFunding <> "All Funding Type" Then

Form_sfrmSpending.Filter = strAT & " AND " & strBur & " AND " & strFT
Form_sfrmSpending.FilterOn = True
Debug.Print Form_sfrmSpending.Filter
End If
End If
End If

我添加了完整的代码来避免任何混乱。

查看更多

提问者
Tyrone
被浏览
45
Andre 2020-02-01 20:17

=在两条代码行中缺少该标志。

你在这里:

strBur = "Bureau='" & Me.cboBur.Value & "'"
                ^

但这里缺少它,从而产生语法错误:

strAT = "Categories '" & Me.cboCategories.Value & "'"
                   ^
strFT = "[Funding Type] '" & Me.cboFunding.Value & "'"
                       ^

另外,您的逻辑太复杂了。您可以If为每个组合框创建一个过滤器字符串

Dim sFilter As String
sFilter = ""

If cboBur <> "All Bureaus" Then
    sFilter = StrAppend(sFilter, strBur, " AND ")
End If
If cboCategories <> "All Categories" Then
    sFilter = StrAppend(sFilter, strAT, " AND ")
End If
' etc.

If sFilter <> "" Then
    Form_sfrmSpending.Filter = sFilter
    Form_sfrmSpending.FilterOn = True
End If

仅在两个字符串都不为空的情况下,才使用包含分隔符的此帮助器函数:

Public Function StrAppend(sBase As String, sAppend As Variant, sSeparator As String) As String

    If Len(sAppend) > 0 Then
        If sBase = "" Then
            StrAppend = Nz(sAppend, "")
        Else
            StrAppend = sBase & sSeparator & Nz(sAppend, "")
        End If
    Else
        StrAppend = sBase
    End If

End Function