When you think ASP, think...
Recent Articles
All Articles
ASP.NET Articles
Message Board
Related Web Technologies
User Tips!
Coding Tips

Sample Chapters
Commonly Asked Message Board Questions
JavaScript Tutorials
MSDN Communities Hub
Official Docs
Stump the SQL Guru!
XML Info
Author an Article
ASP ASP.NET ASP FAQs Message Board Feedback
Print this page.
Published: Thursday, April 15, 1999

Fill A ComboBox with DB Information
By Joao Vieira

Filling a combo box with results a database query is something that ASP developers need to do often. This script will present an example of how to do just this. It's something every beginner should read and implement!

- continued -

First you must open the DB, execute your query, and store the results in a recordset. Once we have that recordset, we will make a loop that puts the values into the combo box. For this example, let's say that the table we're querying has the following structure:


Here is the script. It's pretty simple to follow along. Happy Programming!

    ' the connection object
    Dim objConnection
    ' the recordset object
    Dim objRecordset

    ' create the Connection object
    Set objConnection = Server.CreateObject("ADODB.Connection")
    ' open the DSN

    ' creates the recordset object
    Set objRecordset = Server.CreateObject("ADODB.Recordset")

    ' SQL String for query     Dim strSQL

    ' execute the SQL statement that you want
    ' The query should return all the data you'll
    ' need in your combo box
    objRecordset.Open strSQL, objConnection

    <FORM ACTION="someurl.asp" METHOD=POST>
        Choose from the combo:
        select name="lstTopic" size="1">
        ' while we don't get the end of DataBase
        Do While Not objRecordset.EOF
            'we put the ID at Value
            ' this value is important to linked tables
            <option VALUE="<%=objRecordset("ID")%>">
                <!-- This is what will appear in the combo box -->
            ' Move to the next record...
        Loop ' keep the loop


    ' close the recordset object

    ' clean the Recordset object
    Set objRecordset = Nothing

João Vieira BIO: (email me!)
- Age : 23
- Student And worker (tryin' to finish university, on the last year of the graduation)
- Workin' now at NetGate Portugal
- Programming little stuff since 1989
- Programming HTML since 1996
- Programming JavaScript since 1997, made some experiences with Java.
- Programming ASPs since the Summer of 1997 (yeap, I didn't had nothing to do. Now I love it.)
- Programming ActiveX since 1998
- Webmaster of www.joaovieira.com

ASP.NET [1.x] [2.0] | ASPFAQs.com | Advertise | Feedback | Author an Article