简体   繁体   中英

Searching in more than one column

I have an access database I search through from excel with the help of VBA and ADODB.

It works perfectly fine, but I can only search for one thing at a time. Say I want to search for all instances of "first name" in column [1] that also has "last name" in column [2]. Or, "first name" in column [1] and "address" in column [3].

How would I go about doing that from a textbox on a userform?

I was thinking of somehow adding "Firstname + Lastname" or something, but I cannot figure out the logic of the code. Any suggestions?

This is what I have for now.

Private Sub cmd_lookup_Click()

    Dim cn As Object
    Dim rs As Object

    Set cn = CreateObject("ADODB.Connection")
    Set sqlConnect = New ADODB.Connection
    Set rs = CreateObject("ADODB.RecordSet")

    sqlConnect.ConnectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=Source\database.accdb;Persist Security Info=False;"

    cn.Open sqlConnect

    rs.ActiveConnection = cn

    SearchCriteria = "%" & searchCrit.Text & "%"

    rs.Open "SELECT [1],[2] ,[3],[4], [5] FROM [tblDatabase]" & _
    "WHERE      [1] LIKE '" & SearchCriteria & "' " & _
    "           OR [2] LIKE '" & SearchCriteria & "' " & _
    "           OR [3] LIKE '" & SearchCriteria & "' " & _
    "           OR [4] LIKE '" & SearchCriteria & "' " & _
    "           OR [5] LIKE '" & SearchCriteria & "' " & _
    "ORDER BY [2] Desc;", _
         cn, adOpenStatic

Dim i As Integer
If Not rs.EOF Then
    rs.MoveFirst
    i = 0
    With lstLookup
        'Code not relevant
    End With

    rs.Close
    cn.Close
    Set rs = Nothing
    Set cn = Nothing
  End If
End Sub

Thanks for the suggestions :)

Consider AND parings separated by OR operators. But first have userform structured with separate text fields (and each must be a required field else you must build WHERE clause dynamically):

First Name Search:_____________
Last Name Search: _____________
Address Search: _______________

And then bind criteria together. Also, I would not advise numbered but named fields for debugging reasons:

rs.Open "SELECT [1], [2], [3], [4], [5]" & _
        " FROM [tblDatabase]" & _
        " WHERE ([1] LIKE '%" & FirstNameSearch & "%' " & _
        "       AND [2] LIKE '%" & LastNameSearch & "%') " & _
        " OR ([1] LIKE '%" & FirstNameSearch & "%' " & _
        "      AND [3] LIKE '%" & AddressSearch & "%') " & _
        " OR ([2] LIKE '%" & LastNameSearch & "%' " & _
        "    AND [3] LIKE '%" & AddressSearch & "%') " & _
        " ORDER BY [2] Desc;", _
        cn, adOpenStatic

Alternatively, use a UNION query as often in the SQL world it is discussed the efficiency between UNION vs OR . To be truly equivalent, a DISTINCT should be added to above for both to remove duplicates.

rs.Open "SELECT [1], [2], [3], [4], [5]" & _
        " FROM [tblDatabase]" & _
        " WHERE ([1] LIKE '%" & FirstNameSearch & "%' " & _
        "        AND [2] LIKE '%" & LastNameSearch & "%') " & _
        " UNION" _
        " SELECT [1], [2], [3], [4], [5]" & _
        " FROM [tblDatabase]" & _
        " WHERE ([1] LIKE '%" & FirstNameSearch & "%' " & _
        "        AND [3] LIKE '%" & AddressSearch & "%') " & _
        " UNION" _
        " SELECT [1], [2], [3], [4], [5]" & _
        " FROM [tblDatabase]" & _
        " WHERE ([2] LIKE '%" & LastNameSearch & "%' " & _
        "        AND [3] LIKE '%" & AddressSearch & "%') " & _
        " ORDER BY [2] Desc;", _
        cn, adOpenStatic

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM