Go to: Articles List

How to connect to a database via a DSN-Less connection

Here is what the code looks like:

Dim oConn, sConnString
Set oConn = Server.CreateObject("ADODB.Connection")
sConnString = "DRIVER={Microsoft Access Driver (*.mdb)};" & _
    "DBQ=" & Server.MapPath("\MemberName\db\dbname.mdb") & ";"
oConn.Open(sConnString)

NOTE: PREMIUM Members: Remove "\MemberName" from the connection string.

At this point you can define a SQL statement and execute it:

sql = "select * from myTable"
Set RS = Conn.Execute(sql)

The components of the DSN-Less connection explained:

First we instantiate the Connection Object:

Set Conn = Server.CreateObject("ADODB.Connection")

Next we define the actual connection string:

sConnString = "DRIVER={Microsoft Access Driver (*.mdb)};" & _
    "DBQ=" & Server.MapPath("\MemberName\db\dbname.mdb") & ";"

NOTE: Premium Members: Remove "\MemberName" from the connection string.

Make sure the above line of code is all on one line in your code, don't wrap it. In this statement we are telling ADO (Active Data Objects) that we want to use the MS Access driver and where our database is physically located.

Conn.Open(MyConnStr)

This is the line of code that actually opens the connection to the database. After you have done this you are ready to execute a SQL statement.