By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,652 Members | 1,409 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,652 IT Pros & Developers. It's quick & easy.

Populate Multi select LISTBOX from Database

P: 91
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.
Aug 13 '09 #1
Share this Question
Share on Google+
4 Replies

Expert 100+
P: 189
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.

Aug 14 '09 #2

P: 91

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%>
  8.                          <%IF rs("needlist")= "Food" Then%>
  9.                          <option value="Food" selected>Food</option>
  10.                          <%Else%>
  11.                          <option value="Food">Food</option>
  12.                          <%End IF%>
Aug 14 '09 #3

Expert 100+
P: 189

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"%> >
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?

Aug 14 '09 #4

P: 91
Works well. Thanks for this Gaz.
Aug 14 '09 #5

Post your reply

Sign in to post your reply or Sign up for a free account.