WHERE、AND、OR 组合

WHERE, AND, OR Combination

我有 2 个对象,第一个是客户的列表框,第二个是用作关键字搜索的文本字段

如果用户从列表框中选择客户名称,则会在子表单中为客户选择正确的对应记录。但是当我选择了一个客户名称并使用关键字时,该关键字会搜索所有客户名称的所有记录。我认为我的 Where 子句中的 AND 语句不正确。如果用户选择了客户名称并使用关键字,那么我希望该关键字只搜索选定的客户。以下是我正在使用的 2 个函数。

Private Sub CustomerListbox_Click()

Dim SQL As String

SQL = "SELECT tblPartsAndConsumables.DESCRIPTION, tblPartsAndConsumables.[P/N], tblPartsAndConsumables.[S/N], tblPartsAndConsumables.[B/N], tblPartsAndConsumables.QTY, tblPartsAndConsumables.[EXPIRY DATE], tblPartsAndConsumables.LOCATION, tblPartsAndConsumables.Attachments " _
& " FROM tblPartsAndConsumables " _
& "WHERE [Customer Name] LIKE  '*" & Me.CustomerListbox.Value & "*' " _
& " ORDER BY tblPartsAndConsumables.DESCRIPTION, tblPartsAndConsumables.[P/N]; "

Me.SubFormSearch.Form.RecordSource = SQL
Me.SubFormSearch.Form.Requery

End Sub

Private Sub Searchbox_Change()

Dim SQL As String

SQL = "SELECT tblPartsAndConsumables.[DESCRIPTION], tblPartsAndConsumables.[P/N], tblPartsAndConsumables.[S/N], tblPartsAndConsumables.[B/N], tblPartsAndConsumables.QTY, tblPartsAndConsumables.[EXPIRY DATE], tblPartsAndConsumables.LOCATION, tblPartsAndConsumables.Attachments " _
& " FROM tblPartsAndConsumables " _
& "WHERE [Customer Name] LIKE  '*" & Me.CustomerListbox.Value & "*' " _
& "AND [DESCRIPTION] LIKE  '*" & Me.Searchbox.Text & "*' " _
& "OR [P/N] LIKE  '*" & Me.Searchbox.Text & "*' " _
& "OR [S/N] LIKE  '*" & Me.Searchbox.Text & "*' " _
& "OR [B/N] LIKE  '*" & Me.Searchbox.Text & "*'" _
& " ORDER BY tblPartsAndConsumables.[DESCRIPTION], tblPartsAndConsumables.[P/N]; "

Me.SubFormSearch.Form.RecordSource = SQL
Me.SubFormSearch.Form.Requery

End Sub

你应该用 () 包裹由 OR 条件连接的条件

    SQL = "SELECT tblPartsAndConsumables.[DESCRIPTION], tblPartsAndConsumables.[P/N], tblPartsAndConsumables.[S/N], tblPartsAndConsumables.[B/N], tblPartsAndConsumables.QTY, tblPartsAndConsumables.[EXPIRY DATE], tblPartsAndConsumables.LOCATION, tblPartsAndConsumables.Attachments " _
    & " FROM tblPartsAndConsumables " _
    & "WHERE [Customer Name] LIKE  '*" & Me.CustomerListbox.Value & "*' " _
    & "AND ( [DESCRIPTION] LIKE  '*" & Me.Searchbox.Text & "*' " _
    & "OR [P/N] LIKE  '*" & Me.Searchbox.Text & "*' " _
    & "OR [S/N] LIKE  '*" & Me.Searchbox.Text & "*' " _
    & "OR [B/N] LIKE  '*" & Me.Searchbox.Text & "*' ) " _
    & " ORDER BY tblPartsAndConsumables.[DESCRIPTION], tblPartsAndConsumables.[P/N]; "

您必须使用括号来分隔 where 子句中的可比较项。也许是这样的。

Dim SQL As String

SQL = "SELECT tblPartsAndConsumables.[DESCRIPTION], tblPartsAndConsumables.[P/N], tblPartsAndConsumables.[S/N], tblPartsAndConsumables.[B/N], tblPartsAndConsumables.QTY, tblPartsAndConsumables.[EXPIRY DATE], tblPartsAndConsumables.LOCATION, tblPartsAndConsumables.Attachments " _
& " FROM tblPartsAndConsumables " _
& "WHERE ([Customer Name] LIKE  '*" & Me.CustomerListbox.Value & "*') " _
& "AND ([DESCRIPTION] LIKE  '*" & Me.Searchbox.Text & "*' " _
& "OR [P/N] LIKE  '*" & Me.Searchbox.Text & "*' " _
& "OR [S/N] LIKE  '*" & Me.Searchbox.Text & "*' " _
& "OR [B/N] LIKE  '*" & Me.Searchbox.Text & "*')" _
& " ORDER BY tblPartsAndConsumables.[DESCRIPTION], tblPartsAndConsumables.[P/N]; "