Knowledge Base
Categories: MS_Access
Connecting to a MS Access database
Connect to your MS Access database
Embed a connection string (strConnect) in the body of any script which needs to talk to a database. Here's an example:
'Database connection info and driver
'strCon = "DRIVER={Microsoft Access Driver (*.mdb)}; DBQ=" & Server.MapPath("databasedbname.mdb")
Place this string directly into your code (replacing the databasedbname with the name of your Access database) and your script should work correctly. Here is another example of a script that you may use to connect to your MS Access database:
<%
'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("databasedbname.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("databasedbname.mdb")
'This one is for Access 2000/2002
strCon = "Provider=Microsoft.Jet.OLEDB.4.0; Data Source=" & Server.MapPath("databasedbname.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
%>
NOTE: The Server.MapPath statement makes it unnecessary to know the physical path.