Specifying the Data Source

The Connection object has a property, ConnectionString, which you can use to inform ADO where to find the data source, as well as information about opening it. Here's a simple example:

Dim cnConnectToLedger As New ADODB.Connection
With cnConnectToLedger
    .Provider = "Microsoft.Jet.OLEDB.4.0"
    .ConnectionString = _
    "Data Source=C:\Documents and Settings\GL.mdb"
End With

Building More Complicated Connections

The ConnectionString is a wide-ranging property and typifies the flexibility of ADO: You can use it to specify aspects of the connection that can also be specified elsewhere. The example given earlier specified the provider using the Provider property. It could also have been managed this way:

 Dim cnConnectToLedger ...

Get Managing Data with Microsoft® Excel now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.