0

How can I make this code Search ANY field under the textboxes? right now it will only return a result if all fields match exactly? Thanks in advance!

Private Sub Button1_Click_2(sender As Object, e As EventArgs) Handles Button1.Click
    cmd = New SqlCommand
    cmd.Connection = cn
    cmd.CommandText = "Select * from [Case Managers] " & _
          "where firstname like '%" & Me.txtfirstname.Text & "%' " &
          "or " & _
          "lastname like '%" & Me.txtlastname.Text & "%' " & _
          "or " & _
          "credentials like '%" & Me.txtCredentials.Text & "%'" & _
          "or " & _
          "active like '%" & Me.cboActive.Text & "%'"
    dr = cmd.ExecuteReader

    Me.ListView1.Items.Clear()
    While dr.Read
        With Me.ListView1
            .Items.Add(dr(0))
            With .Items(.Items.Count - 1).SubItems
                .Add(dr(1))
                .Add(dr(2))
                .Add(dr(3))
                .Add(If(dr(4) = 0, "No", "Yes"))
            End With
        End With
    End While
    dr.Close()
End Sub

End Class

InFeKtId
  • 13
  • 4
  • I was looking for `AND` according to your question, but I found `OR`. This looks like it should work... – djv Sep 18 '14 at 21:54
  • 1
    Your query has so many problems, but surely not the one you mention (Real problems are Sql Injection, Names with quotes, global connection variable, connection not closed at the end of the query) – Steve Sep 18 '14 at 22:02
  • Here is the FUll Code If someone wants to see it ALL or Clean it up for me :) http://expirebox.com/download/11660e0456252c6411268f78dab7e891.html – InFeKtId Sep 18 '14 at 22:12

1 Answers1

0

You can use OR: WHERE field1 like or = whatever1 OR field2 like or equal whatever2 ...

http://www.sqlservercentral.com/Forums/Topic750935-338-1.aspx

or you can use concat: concatenate results from the fields, and do a like:

search a whole table in mySQL for a string

Community
  • 1
  • 1
Topological Sort
  • 2,733
  • 2
  • 27
  • 54