vba - 在过滤器中使用 "Or"

标签 vba ms-access search filter subform

我有一个搜索按钮,单击该按钮会显示子表单中与父表单中文本框中的值匹配的记录。我有一个单选按钮可以在“并搜索”(如果匹配所有条件)和“或搜索”(如果匹配任何一个条件)之间进行选择。我正在使用过滤器来显示结果。这对于“并搜索”非常有效,但不适用于“或搜索”。在“或搜索”期间,无论文本框中有任何值,都会显示所有记录。 我的代码如下。 希望能在这里找到解决办法。谢谢。

    Private Sub Form_Open(Cancel As Integer)

    Dim StrId, StrName, FilterId, FilterName, AndOr As String
    Dim IntRadio as Integer

    If Not IsNull(Forms!ParentForm!TxId.Value) Then
        StrId = Forms!ParentForm!TxId.Value
    End If

    FilterId = "[BookID] Like '*" & StrId & "*'"
    FilterName = "[BookName] Like '*" & StrName & "*'"

    If IntRadio = 1 Then
        AndOr = " and "
    Else
        AndOr = " or "
    End If

    Me.Filter = FilterId & AndOr & FilterName 
    Me.FilterOn = True
        Me.Requery

    If Me.RecordsetClone.RecordCount = 0 Then
        Cancel = True
        MsgBox "No Records", vbOKOnly + vbInformation, "No Data"
    End If

End Sub

最佳答案

Private Sub Form_Open(Cancel As Integer)

Dim StrId, StrName, StrFilter, AndOr As String
Dim IntRadio as Integer

If IntRadio = 1 Then
    AndOr = " and "
Else
    AndOr = " or "
End If

If Not IsNull(Forms!ParentForm!TxId.Value) Then
    StrId = Forms!ParentForm!TxId.Value
    StrFilter = StrFilter & "[BookID] Like '*" & StrId & "*'" & AndOr
End If

If Not IsNull(Forms!ParentForm!TxName.Value) Then
    StrId = Forms!ParentForm!TxName.Value
    StrFilter = StrFilter & "[BookName] Like '*" & StrName & "*'" & AndOr
End If  

StrFilter = Left(StrFilter, Len(StrFilter) - 4)

Me.Filter = StrFilter

Me.FilterOn = True
Me.Requery

If Me.RecordsetClone.RecordCount = 0 Then
    Cancel = True
    MsgBox "No Records", vbOKOnly + vbInformation, "No Data"
End If
End Sub

关于vba - 在过滤器中使用 "Or",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33162637/

相关文章:

java - 通过 JDBC-ODBC 从 Access 数据库读取 Unicode 字符

ms-access - 将 .MDB 文件拆分成段是否有助于提高稳定性?

android - 升序排列的位置列表

java - 向 Google map 中的搜索框添加条件?

java - 在 Android 中搜索带有关键字的任何网页

vba Vlookup类型不匹配

excel - 如何在VBA中找到最小的数组,而不是在excel范围内

vba - Concat 宏不起作用,编码错误

excel - 'For' 循环中的平均值

ms-access - 循环遍历名称类似于示例的查询