Sep 10

Using Microsoft Access databases (.mdb) from ASP code

Employees don't always have the pleasure to choose which environment to use. So sometimes you have to learn technologies you have always avoided.. sigh!

Skeleton/template of an ASP page connecting and querying a Microsoft Access (.mdb) database:

<%
Option Explicit

Dim conn
set conn=Server.CreateObject("ADODB.Connection")
conn.Open "Provider=Microsoft.Jet.OLEDB.4.0; Data Source=" & Server.MapPath("YOUR DB FILE.mdb")

Dim rs
Set rs = Server.CreateObject ("ADODB.Recordset")

Dim sql
sql = "YOUR SQL CODE"

rs.open sql,conn

if rs.bof AND rs.eof then
   ' Empty recordSet, do what you need
end if

while not rs.EOF
  ' Do what you want with the current tuple
  ' Access columns with rs("COLUMN NAME")

  rs.movenext
wend

rs.Close
conn.Close
Set rs = Nothing
Set conn = Nothing
%>