"SELECT * FROM customer where customer_ID LIKE '%" & Trim(TextBox1.Text) & "%'"

good day everyone, im having a problem with querrying

i want to query from the field customer_ID,customer_Name,customer_MI,customerLname and textbox 1, 2, 3 and 4

i already tried

select * from customer where customer id like textbox1.text or customer_name textbox2.text blaa blaa blaa

like that but it aint working,please teach me how to do it properly

thank you and god bless

6 Years
Discussion Span
Last Post by crapulency

Hi Skillzilog. When you say it isn't working, do you mean you are getting some kind of error, or are your queries just not returning the records you think they should?

(btw Is CUstomer_ID a numeric field? If so, you cannot use LIKE without first converting it into a string- although my guess is you'd be better of using = instead for this field)


sir it is working when i query it individually, customer_id is a autonumber but it is working, my main problem is how can i query multiple fields?


try this:

Private Function BuildSQL() As String
        Dim sql As String = String.Empty

        If TextBox1.Text <> String.Empty Then
            sql = "customer_ID = " & TextBox1.Text.Trim
        End If
        If TextBox2.Text <> String.Empty Then
            AddCondition(sql, "customer_Name LIKE '%" & TextBox2.Text.Trim & "%'")
        End If
        If TextBox3.Text <> String.Empty Then
            AddCondition(sql, "customer_MI LIKE '%" & TextBox3.Text.Trim & "%'")
        End If
        If TextBox4.Text <> String.Empty Then
            AddCondition(sql, "customer_lname LIKE '%" & TextBox4.Text.Trim & "%'")
        End If
        If sql <> String.Empty Then
            sql = "SELECT * FROM customer WHERE " & sql
        End If
        Return sql
    End Function

    Private Sub AddCondition(ByRef sql As String, ByVal condition As String)
        If sql = String.Empty Then
            sql = condition
            sql &= " OR " & condition
        End If
    End Sub

These functions will build your sql statement or return empty string if textboxes are empty. I have assumed customer_MI is a text field also

This article has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.