• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 385
  • Last Modified:

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?
0
Craig_Muckleston
Asked:
Craig_Muckleston
  • 2
  • 2
1 Solution
 
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 SasidharanCommented:
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 SasidharanCommented:
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
 
Diane258Commented:
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

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now