Dependent dropdowns with Classic ASP and Access

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.

Select the dependent list with a parameter

VBScript example

Set cmd = Server.CreateObject("ADODB.Command")
Set cmd.ActiveConnection = cn
cmd.CommandType = 1 ' adCmdText
cmd.CommandText = "SELECT emp_no, name FROM emp_m WHERE dept = ? ORDER BY name"
cmd.Parameters.Append cmd.CreateParameter("pDept", 202, 1, 50, dept)
Set rs = cmd.Execute

The department comes from validated request data; cn is an open Access connection. ADO OLE DB question-mark parameters are positional, so append them in SQL order.

Important: The linked downloadable legacy ZIP may contain older query construction. Adapt it to parameterized SQL and output encoding before production use.
On this page — Show page sectionsHide page sections

A dependent dropdown changes the second set of choices when the first value changes. This legacy Access-backed Classic ASP example reloads the page with the selected department in the query string; modern applications can also fetch the second list asynchronously. For new server-side applications, use a supported server database rather than choosing Access as the web application's primary database.

Reload with the selected department

<script>
document.getElementById("dept").addEventListener("change", function () {
  var value = encodeURIComponent(this.value);
  window.location.href = "emp.asp?dept=" + value;
});
</script>

Resulting URL

https://sitename.com/emp.asp?dept=sales

Read the selected value

dept = Trim(Request.QueryString("dept"))

Open the Access database

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")

Load the first dropdown

strSQL = "SELECT DISTINCT dept FROM emp_m ORDER BY dept"
objRS.Open strSQL, objconn
Response.Write "<select id=""dept"" name=""dept"">"
Response.Write "<option value="""">Select department</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

Load matching employees safely

If Len(dept) > 0 Then
Set cmd = Server.CreateObject("ADODB.Command")
  Set cmd.ActiveConnection = objconn
cmd.CommandText = "SELECT emp_no, name, dept FROM emp_m WHERE dept = ? ORDER BY name"
cmd.CommandType = adCmdText
cmd.Parameters.Append cmd.CreateParameter("pDept", adVarWChar, adParamInput, 50, dept)
  Set objRS = cmd.Execute
Do While Not objRS.EOF
Response.Write Server.HTMLEncode(CStr(objRS("emp_no"))) & " " & _
Server.HTMLEncode(CStr(objRS("name"))) & " " & _
Server.HTMLEncode(CStr(objRS("dept"))) & "<br>"
objRS.MoveNext
Loop
objRS.Close
End If

The selected department is passed to ADO as a parameter instead of being concatenated into the SQL string. For the ACE OLE DB provider, ? placeholders are positional, so append parameters in the same order they appear in the command.

Download the original ASP/Access example package. Treat the download as legacy reference code and apply the parameterization and output-encoding updates shown on this page before production use.

Avoid SQL injection

Do not build WHERE dept = ... by concatenating Request.QueryString. Bind the requested department as an ADO parameter and encode the resulting HTML output.

Keep the current selection

When the page reloads, set the department option matching the request value as selected. Use a normal form submit as a non-JavaScript fallback to the change event.

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