Newbie: Look up data based on CheckedListBox

  • Thread starter Thread starter Irongrip
  • Start date Start date
I

Irongrip

Hi

I have a database where data is grouped by project name (ie. There is a
ProjectName field with the project name in it). That works fine. I am now
trying to add summary-creation functionality to my app.

I have set up a CheckedListBox containing all the project names, so that
users can select all the projects they want to include in the summary.
However, I have absolutely no idea how to determine what they've selected or
how to display the appropriate data.

I can make a summary of all projects with 'SELECT * FROM Main;' so now I'm
trying to get something along the lines of 'SELECT * FROM Main WHERE
ProjectName = Whatever The User Chooses;'

This would be easy if the user could only select one project at a time, but
in reality I want them to be able to select as many projects as they like.

Hopefully this all made sense. Anyone have any advice?

Thanks
 
August 10, 2004

You can use the listbox's SelectedItems property, to retrieve what
items
the user has selected. It returns an array in the form of a Windows.Forms.-
Listbox.SelectedObjectCollection. Try using the following code...

Dim list As System.Windows.Forms.ListBox.SelectedObjectCollection = _
ListBox1.SelectedItems ' create and fill list

Dim i As Integer
For i = 0 To list.Count - 1 ' - 1 because of zero-based collection
MsgBox(list.Item(i)) ' Do something
Next

I am not sure how to build the SQL statement, but as strictly a guess...


dim selectstatement as string = "'SELECT * FROM Main WHERE ProjectName='"

Dim list As System.Windows.Forms.ListBox.SelectedObjectCollection = _
ListBox1.SelectedItems ' create and fill list

Dim i As Integer
For i = 0 To list.Count - 1 ' - 1 because of zero-based collection

if i = list.count - 1 then ' do not add "OR ProjectName='"
selectstatement &= list.item(i) & "'"
else
selectstatement &= list.item(i) & "' OR ProjectName='"
end if

Next

This should build the SQL statement if my theory of the syntax is
correct. This code is a little hard for eyes to adjust to, but I hope
it helps!


Joseph MCP
 
Thanks, I'll give that a go.


August 10, 2004

You can use the listbox's SelectedItems property, to retrieve what
items
the user has selected. It returns an array in the form of a Windows.Forms.-
Listbox.SelectedObjectCollection. Try using the following code...

Dim list As System.Windows.Forms.ListBox.SelectedObjectCollection = _
ListBox1.SelectedItems ' create and fill list

Dim i As Integer
For i = 0 To list.Count - 1 ' - 1 because of zero-based collection
MsgBox(list.Item(i)) ' Do something
Next

I am not sure how to build the SQL statement, but as strictly a guess...


dim selectstatement as string = "'SELECT * FROM Main WHERE ProjectName='"

Dim list As System.Windows.Forms.ListBox.SelectedObjectCollection = _
ListBox1.SelectedItems ' create and fill list

Dim i As Integer
For i = 0 To list.Count - 1 ' - 1 because of zero-based collection

if i = list.count - 1 then ' do not add "OR ProjectName='"
selectstatement &= list.item(i) & "'"
else
selectstatement &= list.item(i) & "' OR ProjectName='"
end if

Next

This should build the SQL statement if my theory of the syntax is
correct. This code is a little hard for eyes to adjust to, but I hope
it helps!


Joseph MCP
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top