Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Showing data in a combo box from a query

Status
Not open for further replies.

rchen

IS-IT--Management
Mar 7, 2003
16
0
0
SG
I am new to ASP, but know SQL and VBA well. I want to show the results of a query in a combo box on an asp page. If someone could help me with this code, it would be greatly appreciated.

 
Try this:

<%
Dim con, rs, sql
Set con = Server.CreateObject(&quot;ADODB.Connection&quot;)
con.Open connection_string
sql = &quot;your sql statement to select records&quot;
Set rs = Server.CreateObject(&quot;ADODB.Recordset&quot;)
rs.Open con, sql
%>
<select name=&quot;lstList&quot;>
<%
Do Until rs.EOF
Response.Write &quot;<option value='&quot; & rs(&quot;value field&quot;) & &quot;'>&quot; & rs(&quot;display field&quot;)
rs.MoveNext
Loop
rs.Close
Set rs = Nothing
Set con = Nothing
%>
</select>


In the above, you have to create you connection string to open a connection to your database. (Let me know if you need an example).
Also, in the combo box, I am assuming (as this is what I normally do) that you want one thing displayed in the combo box, but another thing to act as the value (eg display names in the combo box, but return the userID as the value of the selection), so will need to select these two fields in the sql statement.

If you need clarification on any of the above, let me know.

Simon
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top