在过滤器中使用 "Or"
Using "Or" in Filter
我有一个搜索按钮,单击该按钮会在子表单中显示与父表单文本框中的值相匹配的记录。我有一个单选按钮可以在 "And Search"(如果符合所有条件)和 "Or Search"(如果符合任何一个条件)之间进行选择。我正在使用过滤器来显示结果。这适用于 "And Search" 但不适用于 "Or Search"。在 "Or Search" 期间显示所有记录,尽管文本框中有任何值。
我的代码如下。
希望能在这里找到解决办法。谢谢。
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
您在两个不同的场合拼写 filtername
不同。一个 filter
和一个 fltr
。我将更改为以下实例,请注意我将 I 和 E 放入。
FilterName = "[BookName] Like '*" & StrName & "*'"
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
我有一个搜索按钮,单击该按钮会在子表单中显示与父表单文本框中的值相匹配的记录。我有一个单选按钮可以在 "And Search"(如果符合所有条件)和 "Or Search"(如果符合任何一个条件)之间进行选择。我正在使用过滤器来显示结果。这适用于 "And Search" 但不适用于 "Or Search"。在 "Or Search" 期间显示所有记录,尽管文本框中有任何值。 我的代码如下。 希望能在这里找到解决办法。谢谢。
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
您在两个不同的场合拼写 filtername
不同。一个 filter
和一个 fltr
。我将更改为以下实例,请注意我将 I 和 E 放入。
FilterName = "[BookName] Like '*" & StrName & "*'"
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