我可以将我的查询转换为使用select .. like ..或?

时间:2017-02-14 00:03:23

标签: sql access-vba

以下代码中的查询是否可以转换为 Select / Like / Or

Private Sub cmdQDef_Click()
    Dim qd As DAO.QueryDef, db As DAO.Database
    Dim ssql As String, WhereName As String, WhereTitle As String

    Set db = CurrentDb

    If Me.FilterName & "" = "" Then
        DoCmd.OpenQuery "q_Search_qdef"
        Exit Sub
    Else
    End If

    ssql = "Select * From Employees"
    Set qd = db.QueryDefs("q_Search_qdef")

    WhereName = "'" & Replace(Me.FilterName, ",", "','") & "'"
    WhereTitle = "'" & Replace(Me.FilterTitle, ",", "','") & "'"    

    ssql = ssql & " Where [First name] In(" & WhereName & ")AND " & _
                     "[Job Title] In (" & WhereTitle & ")"
    qd.SQL = ssql

    DoCmd.OpenQuery "q_Search_qdef"
End Sub

1 个答案:

答案 0 :(得分:0)

是:

WhereName = "'*" & Replace(Me.FilterName, ",", "','") & "*'"
WhereTitle = "'" & Replace(Me.FilterTitle, ",", "','") & "'"    

ssql = ssql & " Where ([First name] Like " & WhereName & ") OR " & _
                 "([Job Title] In (" & WhereTitle & ")"