Displaying Access records with ADO

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.

Read rows with a Recordset

VBScript example

Set rs = cn.Execute("SELECT emp_no, name FROM emp_m ORDER BY emp_no")
Do While Not rs.EOF
  Response.Write Server.HTMLEncode(CStr(rs("name"))) & "<br>"
  rs.MoveNext
Loop
rs.Close

Here cn is an open ADO connection (see the Access connection lesson). Encode database text before inserting it into HTML.

On this page — Show page sectionsHide page sections

This page keeps the original Access/ADO example for legacy-maintenance learning. For a new server-side application, prefer a server database such as SQL Server. If you are maintaining an existing Access-backed ASP site, loop through the returned Recordset until EOF becomes true and encode values before sending them to HTML.

Open the Access connection

Set objconn = Server.CreateObject("ADODB.Connection")
objconn.ConnectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" & _
Server.MapPath("db/emp.mdb")
objconn.Open

Select the fields to display

strSQL = "SELECT emp_no, name FROM emp_m ORDER BY emp_no"

Write each row

Response.Write Server.HTMLEncode(CStr(objRS("emp_no"))) & " " & _
Server.HTMLEncode(CStr(objRS("name"))) & "<br>"

Example output

145 John Reid
245 Peter Horn
175 Rabin Pack
187 Ran Keth

Complete Classic ASP example

Dim objconn, objRS, strSQL
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 emp_no, name FROM emp_m ORDER BY emp_no"
objRS.Open strSQL, objconn
Do While Not objRS.EOF
Response.Write Server.HTMLEncode(CStr(objRS("emp_no"))) & " " & _
Server.HTMLEncode(CStr(objRS("name"))) & "<br>"
objRS.MoveNext
Loop
objRS.Close
Set objRS = Nothing
objconn.Close
Set objconn = Nothing

Server.HTMLEncode() is used here because database content can still contain characters that should not be interpreted as HTML.

Handle an empty result

Before reading a field, test rs.EOF. For lists, a Do While Not rs.EOF loop naturally skips the body when no rows match. Do not rely on RecordCount for forward-only cursors.

Output security

Always HTML-encode database-supplied text before rendering it into the response, including names and other fields that may have originated as form input. See displaying SQL Server rows for the equivalent workflow.

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