我有一个表单,上面有很多控件。我想创建自定义过滤器,允许用户使用任何字段值作为搜索条件来搜索表单上的任何记录。 例如:用户可以使用id搜索记录或者可以使用成绩搜索等
我现在已经编写了以下代码:
Private Sub CmdFind_Click()
Dim filterStr As String
Dim strWhere As String
filterStr = InputBox("Enter your filter criteria")
strWhere = "[SalesOrderNumber] = '" & filterStr & "' "
Me.Filter = strWhere
Me.FilterOn = True
End Sub
但是,这仅搜索“SalesOrderNumber”。我希望该功能也可以使用其他值进行搜索。 任何帮助将不胜感激。谢谢你
据我所知。您想要搜索多个字段。
在这种情况下,您可以使用 OR 子句:
Private Sub CmdFind_Click()
Dim filterStr As String
filterStr = InputBox("Enter your filter criteria")
Dim filters(0 To 2) As String
filters(0) = BuildCriteria("SalesOrderNumber", dbText, filterStr)
filters(1) = BuildCriteria("UserPhoneNumber", dbText, filterStr)
filters(2) = BuildCriteria("Comments", dbText, filterStr)
Me.Filter = Join(filters, " OR ")
Me.FilterOn = True
End Sub
对于需要使用“包含”进行过滤的任何人:
filterStr = SearchInput.Value
Dim filters(0 To 3) As String
filters(0) = "[FirstName] Like '*" & filterStr & "*' "
filters(1) = "[LastName] Like '*" & filterStr & "*' "
filters(2) = "[BirthPlace] Like '*" & filterStr & "*' "
filters(3) = "[BirthDate] Like '*" & filterStr & "*' "
Me.Filter = Join(filters, " OR ")
Me.FilterOn = True
使用(喜欢)并且不要忘记(*)