如何通过文本框过滤数据表视图中的子表单? #likeoperator #where 子句
Posted
技术标签:
【中文标题】如何通过文本框过滤数据表视图中的子表单? #likeoperator #where 子句【英文标题】:How to filter subform in datasheet view by textbox? #likeoperator #whereclause 【发布时间】:2017-07-25 06:28:06 【问题描述】:按下搜索按钮时,我尝试根据主窗体上的文本框的选项框过滤数据表视图中的子窗体。到目前为止,我尝试了 2 种方法:
1) 更改子表单基于在 where 子句中使用 like 运算符的选择查询 -> 不起作用(where 子句有问题)
2) 使用like运算符更改子表单过滤器事件的过滤器 -> 不起作用(错误:“类型不匹配”;在以下代码中注释掉)
感谢任何提示/建议!
这是我的搜索按钮点击事件代码:
Private Sub btnSearch_Click()
Dim strSubQry, strFilter, SearchField As String
On Error GoTo Err_btnSearch_Click
SearchField = Me.txtSearchBox
If IsNull(Me.txtSearchBox) Or Me.txtSearchBox = "" Then
MsgBox "Please enter keyword before searching!", vbOKOnly
Me.txtSearchBox.SetFocus
Else
Select Case Me.FrameSearchOptions
Case Is = 1 'Order No Intern
strSubQry = " SELECT tblShipment.ShipID, tblOrder.OrderNoIntern, tblOrder.CustomerName, tblOrder.CustomerPO, tblOrder.CustomerPN, tblShipment.ShipQTY, " & _
" FROM tblOrder INNER JOIN tblShipment " & _
" ON tblOrder.OrderID = tblShipment.OrderID_FK " & _
" WHERE ((tblOrder.[OrderNoIntern] Like " & SearchField & "*""));"
' strFilter = "[OrderNoIntern]" Like " & Chr(34) & SearchField & " * " & Chr(34)"
Case Is = 2 'Customer Name
strSubQry = " SELECT tblShipment.ShipID, tblOrder.OrderNoIntern, tblOrder.CustomerName, tblOrder.CustomerPO, tblOrder.CustomerPN, tblShipment.ShipQTY, " & _
" FROM tblOrder INNER JOIN tblShipment " & _
" ON tblOrder.OrderID = tblShipment.OrderID_FK " & _
" WHERE ((tblOrder.[CustomerName] Like " & SearchField & "*""));"
' strFilter = "[CustomerName]" Like "& Chr(34) & SearchField & " * " & Chr(34)"
Case Is = 3 'Customer PO
strSubQry = " SELECT tblShipment.ShipID, tblOrder.OrderNoIntern, tblOrder.CustomerName, tblOrder.CustomerPO, tblOrder.CustomerPN, tblShipment.ShipQTY, " & _
" FROM tblOrder INNER JOIN tblShipment " & _
" ON tblOrder.OrderID = tblShipment.OrderID_FK " & _
" WHERE ((tblOrder.[CustomerPO] Like " & SearchField & "*""));"
' strFilter = "[CustomerPO]" Like " & Chr(34) & SearchField & " * " & Chr(34)"
Case Is = 4 'Customer P/N
strSubQry = " SELECT tblShipment.ShipID, tblOrder.OrderNoIntern, tblOrder.CustomerName, tblOrder.CustomerPO, tblOrder.CustomerPN, tblShipment.ShipQTY, " & _
" FROM tblOrder INNER JOIN tblShipment " & _
" ON tblOrder.OrderID = tblShipment.OrderID_FK " & _
" WHERE ((tblOrder.[CustomerPN] Like " & SearchField & "*""));"
' strFilter = "[CustomerPN]" Like " & Chr(34) & SearchField & " * " & Chr(34)"
End Select
End If
Me.sfrmShipmentsDS.Form.RecordSource = strSubQry
Me.sfrmShipmentsDS.Requery
'Me.sfrmShipmentsDS.Form.Filter = strFilter
'Me.sfrmShipmentsDS.Form.FilterOn = True
Exit_btnSearch_Click:
Exit Sub
Err_btnSearch_Click:
MsgBox Error$
Resume Exit_btnSearch_Click
End Sub
【问题讨论】:
【参考方案1】:您正在搜索字符串值,但没有正确转义它们,所以这一行
" WHERE ((tblOrder.[OrderNoIntern] Like " & SearchField & "*""));"
应该是
" WHERE tblOrder.[OrderNoIntern] Like '" & SearchField & "*';"
除非您将多个 AND 和 OR where 子句组合在一起,否则您可能会丢失括号。 您注释掉的行几乎是正确的,但同样构造不正确。
【讨论】:
非常感谢!我知道我有一个小错误,但不明白是什么以及为什么。现在我明白了!当我有多个 where 条件时,是否需要分组的 where 子句? tbh 在构建 where 子句时,我根本不关心括号,除非它会影响 where 子句的应用方式。只有当您混合使用多个 AND 和 OR 标准时,它才会发挥作用。 Access 在创建 SQL 语句时痴迷于添加数百万个括号,大多数情况下是不必要的。 好吧...是的,我就是从那里得到它们的。所以我很困惑 ;-) 再次非常感谢! 我可以再问一个问题:您认为这种代码可以在性能方面进行优化,还是在更改记录源时不会影响性能? 我会确保您的所有搜索字段都在表级别进行索引。一般来说,受 where 子句限制的记录集比加载整个记录集然后应用过滤器要高效得多。您没有这样做,但如果可能的话,请避免在开头带有通配符的 where 子句(Like *Sometext* )
。初始通配符搜索不使用索引,并且很快变得非常缓慢。以上是关于如何通过文本框过滤数据表视图中的子表单? #likeoperator #where 子句的主要内容,如果未能解决你的问题,请参考以下文章