Discussion Locked

multi value listboxes using SQL and ASP

0 helpful
5 replies
Closed to new repliesThis discussion is closed. It stays readable, but it takes no new replies.
What is an efficient way to process a SQL query,using a stored procedure from an ASP page, which allows a user to select single or muli-values from a listbox? The ASP page has 3 parameters (listboxes) from which the user may select one or more values in the listbox. The query results are used to formulate a report.

5 replies

by aking-robertson

I've done this using a VB Component. Once the page has been submitted the values are retrieved in the VB Component which then do the processing. If you have a listbox that allows the user to select mutiple entries, all values will be passed. (It will look something like 1,3,5,7). you can then loop through this and use a string manipulation function to get the individual values. For example:
intLength = len(ListBoxValue)
y = 1
for i = 1 to intLength
x = mid(ListBoxValue,i,1)
if x = "," then
test = mid(listboxvalue, y, i-y)
y = 1 + 1 (add one to account for the space)
end if
next
This works by setting x = each character in the string one at a time. If it is a comma, that means it's the end of that value. y is used to indicate the start position of the value and test is set to the value between the commas from where the last one ended. You can then use each of these numbers to produce the sql for the query. Simply add in the where clause into the middle part of the loop where x tests true to be a comma. ie. 'where id = ' & test. As you loop through this will just add to the where clause each time. (Of course you wouldn't want to actually say 'where' in the loop or it would say it more than once.) I generally make a variable to hold the where clause of the sql statement and then once I've got all the criteria correct I write out the sql statement and include the variable for the where clause at the end. I hope this has helped.

by steved

This is the approach I would take. Say you have two multi-select lists (for the sake of shortening the example) corresponding to two parameters in your SQL query. I'm assuming that you're going to filter your Query based on matching any one of the selections in one of the lists AND any of the selections in each of the other lists. If you logic is different, you'll have to change some of my ANDs to ORs and vice versa.

Your code in VBScript will look something like this:

------------------------
SQL = "SELECT * FROM myTable WHERE ("
If Request.Form("ListBox1").Count > 0 Then
For Each Item in Request.Form("ListBox1")
SQL = SQL & "Param 1 = '" & Item & "' OR "
Next
SQL = Left(SQL, Len(SQL) - 3) & ") AND (" ' get rid of the last OR
End If
If Request.Form("ListBox2").Count > 0 Then
For Each Item in Request.Form("ListBox2")
SQL = SQL & "Param 2 = '" & Item & "' OR "
Next
SQL = Left(SQL, Len(SQL) - 3) & ")" ' get rid of the last OR
Else
SQL = Left(SQL, Len(SQL) - 5) ' get rid of the AND
End If
Set myRecordSet = myConnection.Execute(SQL)
------------------

If you wish to use a stored procedure, you'll have to specify a variable number (possibly large) of optional parameters or else build strings by concatenation (as in my code) using your own convention for delimiting the values and let your stored procedure parse the results.

I hope this helps.

Steve Diamond

This discussion is closed to new replies.