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

Populating a multiple select box from a database

P: 37
Hi

I have a HTML form with a multiple select box. The multiple select box is populated from a database. A user can select multiple options form this select box and the infomrtaion is then stored in a database.

The user then has the option to edit their saved record. How do i populate this multiple select box from a database with the users selected/saved options highlighted?

I can manage to get it to work if the user simply selected one option but not for multiple options. how do i iterate the record set with the saved options whilst at the same time comparing with a record set that populates the multiple select box? Sample code would be help full.
Feb 15 '07 #1
Share this Question
Share on Google+
7 Replies


shweta123
Expert 100+
P: 692
Hi,

set rs=con.executes(sql)
<OPTION selected></OPTION>
<SELECT multiple>
<%while not rs.EOF%>
<%' Here you can put the condition to decide wheather to display it selected or not%>
<%'If yes%>
<OPTION selected><%=rs.Fields(0)%></OPTION>
<<%'else%>
<OPTION><%=rs.Fields(0)%></OPTION>

<%rs.MoveNext
Wend%>
</SELECT>
Feb 16 '07 #2

P: 37
Hi,

set rs=con.executes(sql)
<OPTION selected></OPTION>
<SELECT multiple>
<%while not rs.EOF%>
<%' Here you can put the condition to decide wheather to display it selected or not%>
<%'If yes%>
<OPTION selected><%=rs.Fields(0)%></OPTION>
<<%'else%>
<OPTION><%=rs.Fields(0)%></OPTION>

<%rs.MoveNext
Wend%>
</SELECT>
Hi,

I dont think the above code will work becuase its not comparing with a second record set with the saved options.

To clarify my point - i have one record set which is used to populate the multiple select box. A second record set has the saved options. The first recordset would need to compare each option value with each saved option value to decide if it should be selected or not.
Feb 16 '07 #3

jhardman
Expert 2.5K+
P: 3,405
To clarify my point - i have one record set which is used to populate the multiple select box. A second record set has the saved options. The first recordset would need to compare each option value with each saved option value to decide if it should be selected or not.
In what format is the data in the two rs's? If I was starting this project I think I would first open the rs with the saved selected options and use that to populate an array. Then I would open the rs with all of the options. As I'm constructing the select box, I would check each value against the array to decide if it is selected or not. If the two lists should always be in the same order, you won't even have to scroll through the array more than once.
Feb 17 '07 #4

shweta123
Expert 100+
P: 692
Hi,

Does both the recordsets contain same no of records?
I am assuming that 2 recordsets are in the same order.
You can write it this way

set rs=con.executes(sql) 'rs contains data to fill the listbox
set rs1=con.executes(sql) 'rs1 contains data with saved options

<OPTION selected></OPTION>
<SELECT multiple>
<%while not rs.EOF%>
<%while not rs1.EOF%>
<% If rs1.fields("Options")="Yes"%>
<OPTION selected><%=rs.Fields(0)%></OPTION>
<%else%>
<OPTION><%=rs.Fields(0)%></OPTION>

<%rs.MoveNext
%rs1.Movenext
Wend%>
</SELECT>
Feb 19 '07 #5

P: 37
Hi,

Does both the recordsets contain same no of records?
I am assuming that 2 recordsets are in the same order.
You can write it this way

set rs=con.executes(sql) 'rs contains data to fill the listbox
set rs1=con.executes(sql) 'rs1 contains data with saved options

<OPTION selected></OPTION>
<SELECT multiple>
<%while not rs.EOF%>
<%while not rs1.EOF%>
<% If rs1.fields("Options")="Yes"%>
<OPTION selected><%=rs.Fields(0)%></OPTION>
<%else%>
<OPTION><%=rs.Fields(0)%></OPTION>

<%rs.MoveNext
%rs1.Movenext
Wend%>
</SELECT>
Hi, Thanks for the suggestions. but I've managed to get it to work using an array.
as suggested by hardman!
Feb 20 '07 #6

polymorphic
P: 28
Hi,

I've run into the same situation described above. It seems that using an array as well as another recordset would be ideal for me. do you have a sample of the code you used to make this work?

Thanks,
cj
Jul 11 '07 #7

shweta123
Expert 100+
P: 692
Hi,

In case you are getting error in the the previous code?

Please refer the code below

<%
Dim rs,rs1,con
con.connectionstring ="........."

'Open connection
con.open

'Sql for Filling Listbox

sql = "Select ................"
set rs = con.execute(sql)

'Sql for extracting user selected options
sql = "Select ................"
set rs1 = con.execute(sql)

while not rs.Eof%>
<SELECT id=select1 multiple name=select1>
<%
'Fill the Listbox
'Put the condition to Keep it Selected ot not

If rs1.Fields(2) = 'yes' %>
<OPTION selected><%rs.Fields(0)</OPTION>
<%else%>
<OPTION ><%rs.Fields(0)</OPTION>
end if

rs.movenext
rs1.movenext
wend
%>
Jul 12 '07 #8

Post your reply

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