Microsoft Access 2010中的多个文本筛选器

时间:2016-06-23 14:31:02

标签: ms-access access-vba ms-access-2010

我想使用两个文本框StaffTotalSearchText1和StaffTotalSearchText2来过滤我的数据。

据我所知,只有最新的DoCmd.ApplyFilter命令应用,这意味着我不知道如何应用两个文本过滤器。

编辑:找到解决方案 - 请参阅Johnny的RecordSource代码 应用过滤器的一种方法是执行加载的数据(RecordSource) - 仍然允许使用“DoCmd.ApplyFilter”。

我创建了一个“锁定过滤器1”按钮。启用时,过滤器适用;禁用时,没有过滤器。

Johhny的答案有正确的代码,可以在加载数据时过滤加载的数据。

2 个答案:

答案 0 :(得分:3)

只需在过滤器事件中使用过滤器中的两个值和相同的代码:

DoCmd.ApplyFilter "", _
 "[Forename] Like '*" & me![StaffTotalSearchText1] & "*'" & _
 " Or [Surname] Like '*" & me![StaffTotalSearchText1] & "*'" & _
 " Or [Forename] Like '*" & me[StaffTotalSearchText2] & "*'" & _
 " Or [Surname] Like '*" & me[StaffTotalSearchText2] & "*'"

修改:在第一个后面应用第二个过滤器:

If Not IsNull(Me![StaffTotalSearchText1]) And Len(Me![StaffTotalSearchText1] & "") <> "" Then
    strFilter = "[Forename] Like '*" & Me![StaffTotalSearchText1] & "*'" & _
              " Or [Surname] Like '*" & Me![StaffTotalSearchText1] & "*'"
    If Not IsNull(Me![StaffTotalSearchText2]) And Len(Me![StaffTotalSearchText2] & "") <> "" Then
        strFilter = "(" & strFilter & ") AND " & _
                  "( [Forename] Like '*" & Me![StaffTotalSearchText2] & "*'" & _
                  " Or [Surname] Like '*" & Me![StaffTotalSearchText2] & "*')"
    End If
End If

DoCmd.ApplyFilter "", strFilter

如果要使用RecordSource,可以使用相同的过滤器,但必须为select命令添加SQL:

strFilter = ""

If Not IsNull(Me![StaffTotalSearchText1]) And Len(Me![StaffTotalSearchText1] & "") <> "" Then
    strFilter = "[Forename] Like '*" & Me![StaffTotalSearchText1] & "*'" & _
              " Or [Surname] Like '*" & Me![StaffTotalSearchText1] & "*'"
    If Not IsNull(Me![StaffTotalSearchText2]) And Len(Me![StaffTotalSearchText2] & "") <> "" Then
        strFilter = "(" & strFilter & ") AND " & _
                  "( [Forename] Like '*" & Me![StaffTotalSearchText2] & "*'" & _
                  " Or [Surname] Like '*" & Me![StaffTotalSearchText2] & "*')"
    End If
End If

If strFilter <> "" Then
    strFilter = "SELECT * FROM StaffTable WHERE " & strFilter
Else
    strFilter = "[StaffTable]"
End If

Me.RecordSource = strFilter

答案 1 :(得分:1)

试试这个:

Me.RecordSource = "SELECT * FROM StaffTable " & _
"WHERE [Forename] Like '*" & [Forms]![StaffTotalQuery]![StaffTotalSearchText1] & "*' " & _
   "Or [Surname] Like '*" & me![StaffTotalSearchText1] & "*' " & _
   "Or [ProfessionalID] Like '*" & me![StaffTotalSearchText1] & "*'"

假设您指向正确的对象(我猜测Forename是在另一个表单上?它是您通过表单名称引用的唯一一个),这应该可以。