在多个字段的访问过滤中打开报告

Opening reports in Access filtering on multiple fields

我正在尝试在 MS Access 2010 中打开一个已根据表单上的九个可能字段进行过滤的报告;但是,如果其中一个字段为空,我希望过滤器忽略该字段。我能够基于 3 个可能的字段执行此操作,方法是使用组合 6 "if statements" 遍历所有可能的组合以查看指定的字段组合是否有信息,然后过滤那些指定的字段。如果只有 3 个字段,这很容易做到。

我现在想对表单上的 9 个字段做同样的事情,但这需要 "if statements" 的 362,880 种组合。是否有另一种方法可以打开报告并仅在这些字段中包含信息时才基于多个字段对其进行过滤?

检查每个搜索文本框,并仅根据值不为 Null 的文本框构建 WhereCondition 字符串。

此示例仅基于 2 个文本框,但可以轻松扩展到更多。

Dim strWhereCondition As String
If Not IsNull(Me.txtSearchID) Then
    ' ID is numeric datatype
    strWhereCondition = strWhereCondition & " AND ID=" & Me.txtSearchID.Value
End If
If Not IsNull(Me.txtSearchDept) Then
    ' Dept is text datatype
    strWhereCondition = strWhereCondition & " AND Dept='" & Me.txtSearchDept.Value & "'"
End If
If Len(strWhereCondition) > 0 Then
    ' discard leading " AND "
    strWhereCondition = Mid(strWhereCondition, 6)
End If
DoCmd.OpenReport "rptFoo", WhereCondition:=strWhereCondition