Powered By Network Solutions: Connecting to an MS Access Database
This guide provides the necessary code examples to help you connect to an MS Access database from your website's scripts. For your script to communicate with the database, you must embed a connection string that contains the correct driver and path information.
Connect to your MS Access Database
To establish a connection, you must embed a connection string (referred to as strCon in the examples) into the body of any script that needs to access the database. Here is a basic example:
'Database connection info and driver 'strCon = "DRIVER={Microsoft Access Driver (*.mdb)}; DBQ=" & Server.MapPath("database\dbname.mdb")
Place this string directly into your code, replacing database\dbname.mdb
with the correct path and name of your Access database. The script below provides a more complete example showing how to connect to an MS Access database, including alternative, faster drivers for Access 97 and Access 2000/2002:
<% 'Holds the recordset Dim rsConfiguration 'Holds the Database driver the path and name of the database Dim strCon 'Create database connection 'Create a connection object Set adoCon = Server.CreateObject("ADODB.Connection") 'Database connection info and driver 'strCon = "DRIVER={Microsoft Access Driver (*.mdb)}; DBQ=" & Server.MapPath("database\dbname.mdb") 'Alternative drivers faster than the generic access one above 'This one is if you use Access 97 'strCon = "Provider=Microsoft.Jet.OLEDB.3.51; Data Source=" & Server.MapPath("database\dbname.mdb") 'This one is for Access 2000/2002 strCon = "Provider=Microsoft.Jet.OLEDB.4.0; Data Source=" & Server.MapPath("database\dbname.mdb") 'Set an active connection to the Connection object adoCon.Open StrCon 'Initialize and ADO recordset object Set rsConfiguration = Server.CreateObject("ADODB.Recordset") 'Perform operations on the database 'Reset server object Set rsConfiguration = Nothing 'Close everything rsConfiguration.close adoCon.close %>
Review
This article has provided the essential connection string examples required to connect to an MS Access database. By embedding the correct `strCon` line with the appropriate driver (either the generic Microsoft Access Driver or the faster Microsoft.Jet.OLEDB provider) into your ASP scripts, you can establish a working connection to your database.