Opening and Closing a Connection: Implicit Versus Explicit
The Connection object is used to establish a unique physical connection to a given data source. This connection defines how you can obtain, interact with, and manipulate data from the specified source. While a Connection object is always required, you can choose whether to instantiate a connection explicitly or to allow ADO to create one implicitly on your behalf.
Opening a Connection
Example 4-1 illustrates how to open a
Recordset object on a table in a data source
without explicitly creating a
Connection
object.
Example 4-1. Implicit Creation of a Connection Object
' declare and instantiate a Recordset
Dim rst As ADODB.Recordset
Set rst = New ADODB.Recordset
' open the Recordset object and implicitly create a Connection
rst.Open "Titles", _
"DSN=BiblioDSN", _
adOpenForwardOnly, _
adLockReadOnly, _
adCmdTable
'
' do something
' close the Recordset and clean up
rst.Close
Set rst = NothingDon’t worry about not understanding the entire example
now -- I will explain everything soon. Do notice, however, how
easy it is to open a table within a data source. Example 4-1 relies on no other code to first establish a
connection; the simple connection string
DSN=BiblioDSN tells ADO that the table, Titles, is
in the BiblioDSN data source.
Some objects in ADO -- in particular, the Recordset and the Command objects -- do not require a pre-existing Connection object to operate. Both objects can read and write data to a data source, and ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access