按下搜索按钮时,我尝试在主窗体上的文本框中根据选项框架过滤数据表视图中的子窗体。到目前为止我尝试了两种方法:
1)改变select查询子表单基于在where子句中使用like运算符 - >不起作用(where子句的问题)
2)使用like运算符更改子表单过滤器事件的过滤器 - >不起作用(错误:"类型不匹配&#34 ;;在以下代码中注释掉)
我感谢任何提示/建议!
这是我的搜索按钮点击事件的代码:
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
答案 0 :(得分:0)
您正在搜索字符串值但未正确转义它们,所以这一行
" WHERE ((tblOrder.[OrderNoIntern] Like " & SearchField & "*""));"
应该是
" WHERE tblOrder.[OrderNoIntern] Like '" & SearchField & "*';"
除非您将多个AND和OR分组到哪个子句中,否则您可能会丢失括号。 你注释掉的行几乎是正确的,但再次没有正确构造。