My understanding of your question is that you have a set of
Checkbox
es and 0 to 5 of them can be checked in any combination.
Ideally you should use an IN-clause. The problem is that it's quite tricky to use an IN clause with a Parameterized query. You should always use parameterized queries over string concatentation.
I created a project with 5 checkboxes, each of which had my search value as the
Text
property. The following works with any number of checkboxes
Using conn As New SqlConnection(constring)
conn.Open()
Using cmd As New SqlCommand()
Dim qry As StringBuilder = New StringBuilder("SELECT * FROM Table_1 ")
Dim whereAdded As Boolean = False
Dim commaRequired As Boolean = False
Dim i As Integer = 1
For Each c As Control In Controls
If c.GetType() = GetType(CheckBox) Then
Dim cb As CheckBox = c
If cb.Checked Then
If Not whereAdded Then
qry.Append("WHERE carnum IN(")
whereAdded = True
End If
If commaRequired Then
qry.Append(",")
End If
qry.Append("@c" + i.ToString())
cmd.Parameters.AddWithValue("@c" + i.ToString(), cb.Text)
i += 1
commaRequired = True
End If
End If
Next
If whereAdded Then
qry.Append(")")
End If
cmd.CommandText = qry.ToString()
cmd.Connection = conn
Dim dr As SqlDataReader = cmd.ExecuteReader()
While (dr.Read())
Debug.Print(dr.GetString(0))
End While
End Using
End Using