Populate a dropdown from Access data

Use this example to see the core idea, then work through the complete steps below. Code snippets are Classic ASP / VBScript or SQL Server T-SQL as labelled, not PHP.

Build option tags from database rows

VBScript example

Response.Write "<select name=""dept"">"
Do While Not rs.EOF
  dept = Server.HTMLEncode(CStr(rs("dept")))
  Response.Write "<option value=""" & dept & """>" & dept & "</option>"
  rs.MoveNext
Loop
Response.Write "</select>"

The example assumes rs contains department names. It HTML-encodes the values before placing them in attributes and text; see the complete ADO example below.

On this page — Show page sectionsHide page sections

This legacy Access-backed example shows the general ADO pattern for building a dropdown from database rows. The same SELECT/loop/output pattern applies when the data source is moved to a supported server database. The query selects each department once with DISTINCT, then writes one HTML <option> for each row.

Write the dropdown options

Response.Write "<select name=""dept"">"
Response.Write "<option value="""">Departments</option>"
Do While Not objRS.EOF
deptText = Server.HTMLEncode(CStr(objRS("dept")))
  Response.Write "<option value=""" & deptText & """>" & deptText & "</option>"
objRS.MoveNext
Loop
Response.Write "</select>"

Complete Access dropdown example

<%@ Language="VBScript" %>
<% Option Explicit %>
<!-- #include virtual="/adovbs.inc" -->
<%
Dim objconn, objRS, strSQL, deptText
Set objconn = Server.CreateObject("ADODB.Connection")
objconn.ConnectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" & _
Server.MapPath("db/emp.mdb")
objconn.Open
Set objRS = Server.CreateObject("ADODB.Recordset")
strSQL = "SELECT DISTINCT dept FROM emp_m ORDER BY dept"
objRS.Open strSQL, objconn
Response.Write "<select name=""dept"">"
Response.Write "<option value="""">Departments</option>"
Do While Not objRS.EOF
deptText = Server.HTMLEncode(CStr(objRS("dept")))
  Response.Write "<option value=""" & deptText & """>" & deptText & "</option>"
objRS.MoveNext
Loop
Response.Write "</select>"
objRS.Close
Set objRS = Nothing
objconn.Close
Set objconn = Nothing
%>

The displayed/attribute value is HTML-encoded before being written. If an option value must later be used in a query, validate it and pass it as a database parameter rather than concatenating it into SQL.

Return safe HTML attributes

Encode both the option value and visible label. If you need to retain the selection, compare the submitted value with the current row value and add a selected attribute only when they match.

The next lesson, dependent dropdowns, uses a department choice to load a matching employee list while keeping SQL parameters separate from user input.

Frequently asked questions

Do these examples require Classic ASP on IIS?
Yes. The server-side code uses VBScript and ADO components from Classic ASP; the .php file here is the Plus2Net tutorial wrapper.
Can I pass form input directly into SQL strings?
No. Validate it and bind it through ADO parameters rather than concatenating it into SQL text.
What should I do if no records match?
Check EOF before accessing fields and show a clear empty-result message.

Back to top ↑


ASP Home






✖
We use cookies to improve your browsing experience. . Learn more
HTML MySQL PHP JavaScript ASP Photoshop Articles Contact us
© 2000-2026 plus2net.com All rights reserved worldwide Privacy Policy Disclaimer