vba - MS Access 过滤问题
问题描述
我在基于组合框自动过滤报告/表单时遇到问题。
这个特定的行:
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
我添加了完整的代码以防万一。
解决方案
您缺少=
两个代码行中的符号。
你在这里:
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
推荐阅读
- python - Selenium 显式等待分页
- rust - 如何在 actix-web 中使用 tokio_postgres
- javascript - 当我鼠标离开菜单时,我无法点击某个地方
- visual-studio-code - VSCode快捷方式选择两个括号/括号之间的代码块?
- java - Completable Future - 使用 Completable Future 我正在尝试来自两个不同文件的数据
- javascript - 打字稿中未捕获的参考错误(VS 2019)
- amazon-web-services - 无法在 AWS 控制台中找到数据库
- arrays - MongoDB 聚合填充/替换 ObjectId 数组
- python - Beautiful Soup 中 find_all 方法的返回类型是什么?
- jquery - 出现垂直滚动条时,Jquery DataTables (1.10) 呈现额外的页脚 (overflow-y: auto)