Link to home
Start Free TrialLog in
Avatar of tommy10101
tommy10101

asked on

ASP - How to assign "selected" values to a combo box

I'm pulling data from a recordset, but I would like the combo box values on my ASP page to automatically "select" the proper value, as listed in the recordset.  This page is used for users to go back and "edit" their account.

For example: The recordset value for the field is "Blue" ... Therefore, in the combo box, the value that's selected will be "Blue".  The order of the combo box values must maintain consistency.

If there's a simple way of doing this without using multiple "If Then" statements, that would be cool.  Thank you!
ASKER CERTIFIED SOLUTION
Avatar of arthuryeung
arthuryeung

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of cwvsmart
cwvsmart

<%
Dim con, rs

'Database connection info and driver (if this driver does not work then comment it out and use 'one of the alternative drivers)
'con = "DRIVER={Microsoft Access Driver (*.mdb)}; DBQ=" & Server.MapPath(yourdatabase.mdb)


'This one is if you convert the database to Access 97    
'strCon = "Provider=Microsoft.Jet.OLEDB.3.51; Data Source=" & Server.MapPath(yourdatabase.mdb)
     
'If you wish to use DSN then comment out the driver above and uncomment the line below (DSN is 'slower than the above drivers)
'con = "DSN=yourDSNname" 'Place the DSN name after the DSN=


'Creating a connection with the Access 2000 Database
'I'm using ACCESS 2000
Set con = Server.CreateObject("ADODB.Connection")
con.Open "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & Server.MapPath("yourdatabase.mdb")


SQL = "SELECT * FROM yourTableName"
Set rs = con.Execute(SQL)
While Not rs.EOF
%>
<Select Name="DropDown">
<option value="<%=rs("fldCategory")%>"><%=rs("fldCategory")%>
     <%
     rs.MoveNext
     Wend
     rs.Close
     %>
</Select>
Try whether this suits you if i understand correctly....
cwvsmart:
i am not sure whether you got the meaning of the question but your code does go wrong:

While Not rs.EOF
%>
<Select Name="DropDown">
<option value="<%=rs("fldCategory")%>"><%=rs("fldCategory")%>
    <%
    rs.MoveNext
    Wend
    rs.Close
    %>
</Select>

you shouldn't put <select xxx> inside the while loop, and it is better to add the close tag </option>, it should be like that:

<Select Name="DropDown">
<%
While Not rs.EOF
%>
<option value="<%=rs("fldCategory")%>"><%=rs("fldCategory")%></option>
    <%
    rs.MoveNext
    Wend
    rs.Close
    %>
</Select>

Avatar of tommy10101

ASKER

Thanks to both of you!  It's working now. Arthur, I accepted your answer as it was the first one that worked for me.  Very simple solution too...

Avatar of CRagsdell
Create a recordset of all colors (rsColors), and another recordset of the user info (rsUsers) that includes the color field, then use similar code to the following to determine if the user color equals the particular color being built into the select option list, and then set the selected tag if they are equal.

Response.Write "<select size=""1"" name=""Color"">"

Response.Write "<option value=""0"">-Select A Color-</option>"

rsColors.MoveFirst

Do While Not rsColors.EOF

Response.Write "<option "

If rsUsers.Fields("Color") = rsColors.Fields("Color") Then
Response.Write "selected "
End If

Response.Write "value="
Response.Write "'" & rsColors.Fields("Color") & "'"">" & rsColors.Fields("Color") & "</option><br>"

rsColors.MoveNext

Loop

Response.Write "</select>"

You will note that I have a "Select A Color" as the first option. You cal leave it out if you want.

Of course, be sure to close the recordsets, etc...

CR
Ooops, too slow on the Submit!

Sorry...

CR