Simple OLEDB connection to Microsoft Access database - ASP Scripts

The following example shows you how to open a Microsoft Access database connection via ASP and its OLEDB 4.0 database drivers.
There are essentially 2 types of connection drivers for Microsoft Access but OLEDB is the most reliable and provide the best performances.

Our sample database is called database.mdb and it is located in the same folder of the file that will open the database connection.
First of all we have to create a new ADODB object instance and define a new variable to hold the database connection string.
Then we simply need to open a new database connection as follows.


' Create a new ADODB instance
Set objConn = Server.CreateObject("ADODB.Connection") 

' Create the connection string
strConn = "Provider=Microsoft.Jet.OLEDB.4.0; Data Source=" & Server.MapPath("database.mdb") 

' Open the connection
objConn.Open strConn 

Please note that the script above provides a relative path to the database using the statement Server.MapPath("database.mdb").

If the database is located in a parent folder starting from the script you simply need to change the path including the classic HTML statement '..' before the path.
For instance, Server.MapPath("../path/to/database.mdb").

If the database is located in a child folder starting from the script you simply need to add the subfolder name to the path.
For instance, Server.MapPath("path/to/database.mdb").

Once the database connection is open you will be able to execute SQL queries but don't forget to close database connection as soon as you don't need it anymore and destroy the object in order to free server resources.


' Close the connection

' Release objects
Set objConn = Nothing 

%> and are property of Simone Carletti.
Discover my website or visit my Simone Carletti's profile on LinkedIn