Creating a SQL Query out of Items checked in a CheckedListBox

I have a CheckedListBox on my Windows Form (built in VB) that gets populated with data from from a table. What I want to be able to do is create a SQL query based on what is checked in the CheckedListBox. For example, If I select Item1 and Item4 from the CheckedList box, I need to create my SQL to look like "SELECT columns from Item1, Item4..."

However, if I only select 1 item then I want this reflected in the SQL statement as well.

Id there a way of cycling through/adding to a colelction list the items in the CheckedListBox? Or is there another method I'm not grasping?
Craig_MucklestonAsked:
Who is Participating?
 
Diane258Connect With a Mentor Commented:
I just tested it out, you can store the results from multiple select statements in the datagrid.

HOWEVER if you are wanting to do a single select statement the format:

Select * from t1 where item=x or item=y or item=z

basicialy you will have to add "or item=whatever" for every selected item in the listbox
0
 
nayernaguibCommented:
The following code can be used to process single checkboxlist items:

        Dim item As ListItem
        For Each item In myList.Items
            If item.Selected Then
                'Process items here
            End If
        Next

To add/remove items:

        myList.Items.Add(string) or
        myList.Items.Add(ListItem)
        myList.Items.Remove(string)
        myList.Items.Remove(ListItem)

________________

  Nayer Naguib
0
 
Shiju SasidharanAssoc Project ManagerCommented:
hi try this
Place a Listbox on ur form
'===============================
Private Sub Form_Load()
    List1.Style = 1
    For i = 1 To 10
        List1.AddItem "Item" & i
    Next
End Sub

Private Sub List1_ItemCheck(Item As Integer)
Dim sQuery As String
Dim i As Integer
    For i = 0 To List1.ListCount - 1
        If List1.Selected(i) Then
            If sQuery = "" Then
                sQuery = List1.List(i)
            Else
                sQuery = sQuery & ", " & List1.List(i)
            End If
        End If
    Next i
    MsgBox "SELECT columns from " & sQuery
End Sub
'===============================

;-)
Shiju
0
 
Shiju SasidharanAssoc Project ManagerCommented:
sorry Craig_Muckleston, there is  one correction
Style property is read only please remove that statement from form load event
use this one

'==================
Private Sub Form_Load()
    For i = 1 To 10
        List1.AddItem "Item" & i
    Next
End Sub
'==================

;-)
Shiju
0
 
Diane258Commented:
Just so i am clear, you are wanting a select statement right?

As i understand it, you can keep all the data in the datagrid and add information to it using another select statement.

since this is the case, simply build a loop like:

If there are no more items in the list, quit
     Else continue to process items from the list
       itemnum=itemnum+1
        if itemnum=selected
           sql="select" +info +"FROM" table
        else
            do nothing
loop
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.