Connecting Tech Pros Worldwide Forums | Help | Site Map

Populate Multi select LISTBOX from Database

Member
 
Join Date: Sep 2008
Posts: 75
#1: Aug 13 '09
Hi,
How can I populate my multi select LISTBOX from access database. The values are seperated by a comma in the column of table. I want the respective OPTIONS in listbox to be SELECTED, when read from database. Thanks.

GazMathias's Avatar
Expert
 
Join Date: Oct 2008
Location: Bristol, United Kingdom
Posts: 145
#2: Aug 14 '09

re: Populate Multi select LISTBOX from Database


Hi there,

Can you show us what you've tried so far? We aim to "point in the right direction" rather than write code for you.

Gaz
Member
 
Join Date: Sep 2008
Posts: 75
#3: Aug 14 '09

re: Populate Multi select LISTBOX from Database


Hi,

this is what i have so far but it will only work if there is one value in the db table, what if there are 2 or more values (seperated by comma in the table), how to fetch them & then dispay in the LISTBOX. Offcourse the values should get SELECTED as well in the listbox. Thanks for your help.
Expand|Select|Wrap|Line Numbers
  1. <select name="needlist" size="5" multiple id="needlist">
  2.                          <%IF rs("needlist")= "Water" Then%>
  3.                          <option value="Water" selected>Water</option>
  4.                          <%Else%>
  5.                          <option value="Water">Water</option>
  6.                          <%End IF%>
  7.  
  8.                          <%IF rs("needlist")= "Food" Then%>
  9.                          <option value="Food" selected>Food</option>
  10.                          <%Else%>
  11.                          <option value="Food">Food</option>
  12.                          <%End IF%>
  13.  
  14.  
GazMathias's Avatar
Expert
 
Join Date: Oct 2008
Location: Bristol, United Kingdom
Posts: 145
#4: Aug 14 '09

re: Populate Multi select LISTBOX from Database


Hi,

The way I see it you can either check for the existence of the value in the field using the insr() function, or you can split() apart the field and iterate it through a for each loop.

1st method:

Expand|Select|Wrap|Line Numbers
  1. <option value = "Food" <%if instr(rs("needlist"),"Food")>0 then response.write  " selected"%> >
  2.  
Second method is harder to describe as I would probably have a loop within a loop.

That is to say, rather than having statically typed values and conditionals in the page I would probably loop those initial select box values from an array (or db, if that's where they are stored), then iterate through the ones in the recordset to select them if necessary.

Probably didn't describe that the best way, so:

1st for each loop (data = the array of values for the listbox)
Put value into <option>
2nd for each loop (data = the rs field)
Check value, write "selected" if found.
Repeat 2nd loop
Repeat 1st loop

This way, every value you enter into the array will be checked, otherwise you have to copy and paste bits of code for each instance. Can get messy if many people can manage those values.

Make sense?

Gaz
Member
 
Join Date: Sep 2008
Posts: 75
#5: Aug 14 '09

re: Populate Multi select LISTBOX from Database


Works well. Thanks for this Gaz.
Cheers.
Reply