Dynamic List Boxes

Sometimes generating a list box from a database can be a really handy (and efficient!) thing to do. By combining database access techniques with a simple Do While...loop statement, we can dynamically generate a list box with very little effort.

Here's a list box generated from the Samples database:

The code to do this is surprisingly simple. First open up your database connection if its not already opened, create your recordset by doing an appropriate query, then do the Do While...Loop to pull data from your recordset and put it into your select options. The Samples database has a Name and ID field that I'm using for this particular example. Here's the Do While...Loop code:

<%
Do while not rs.eof
    ID=rs("ID")
    UserName=rs("name")
    response.write "<option value=""" & ID & """>" & UserName & "</option>"		
    rs.movenext
loop
%>		
And that's all there is to it!