Connect to an Access Database using ODBC

One of the top questions we get asked about ASP is how do you connect to a data store and display data . In this example we will connect to an Access database called example which is in the same directory as this script .

You can download the database used in this example here

Script :

<%
Dim adOpenForwardOnly , adLockReadOnly , adCmdTable
'our ado variables
adOpenForwardOnly = 0
adLockReadOnly = 1
adCmdTable = 2
Dim objConn , objRS
'create instances of Connection object and the Recordset object
Set objConn = Server.CreateObject("ADODB.Connection")
Set objRS = Server.CreateObject("ADODB.Recordset")
'open the example database
objConn.Open "DBQ=" & Server.MapPath("example.mdb")& ";Driver={Microsoft Access Driver (*.mdb)}"
'open the tblexample table
objRS.Open "tblexample" , objConn , adOpenForwardOnly , adLockReadOnly , adCmdTable
'display data in database looping through all entries
While Not objRS.EOF
Response.Write objRS.Fields("id") & "&nbsp"
Response.Write objRS.Fields("link") & "&nbsp"
Response.Write objRS.Fields("description") & "&nbsp"
Response.Write "<p>"
objRS.MoveNext
Wend
'close the recordset
objRS.Close
'destroy all objects
Set objRS = Nothing
Set objConn = Nothing
%>

 

Sponsors