Creating a Connection Object

by Linda Quinn

Close Window

The ADO Connection Object provides a connection to a data store.


A connection object can be created using a Data Source Name [DSN]
or by specify the data store (data source) and data provider.

Dim cn As ADODB.Connection
Set cn = New ADODB.Connection


Set the connection to a DSN data provider

Dim cn As ADODB.Connection
Set cn = New ADODB.Connection

cn.Open "DSN=Products"
       OR
cn.ConnectionString = "DSN=Products"
cn.Open


If DSN isn't used, a Connection String will define the data source and
   data provider.

OLE DB Connection String for an Access Database

Dim cn As ADODB.Connection
Set cn = New ADODB.Connection

cn.ConnectionString = "Microsoft.Jet.OLEDB.4.0:
Data Source=C:\QSPR\Sales.mdb"

cn.Open


You can also build the connection string in the Open method.

Dim cn As ADODB.Connection
Set cn = New ADODB.Connection

cn.Open "Provider=Microsoft.Jet.OLEDB.4.0;" & _
"Data Source=C:\QSPR\Sales.mdb;"



The Connection Mode defines the read/write permissions.

cn.Mode = adModeReadWrite


adModeRead Read-only permission.
adModeWrite Write-only permission.
adModeReadWrite Read and write permission.
adModeUnknown Permissions not defined.


Dim cn As ADODB.Connection
Set cn = New ADODB.Connection

cn.Mode = adModeReadWrite
cn.Open "Provider=Microsoft.Jet.OLEDB.4.0;" & _
"Data Source=C:\QSPR\Sales.mdb;"


See more connection string examples


To see the full connection string after opening:
Debug.Print "Full connection string: " + cn.ConnectionString



=================================================================
This content was created by Linda Quinn of LQNet.

See http://www.lqnet.com for a great collection of articles on this and other topics.

=================================================================
Copyright © 2006-2008, LQ Systems,Inc. All rights reserved.


====================================================================
Want an expert to help with your project?    LQ Systems, Inc.   Business Solutions
====================================================================