The general steps you need to take to initialize a data-connected Recordset
in your code are as follows:
-
Make sure you have a valid Connection or Command object.
-
Declare an object variable of the type ADODB.Recordset.
-
Set the Source property (typically, a SQL statement or the name of a stored procedure
or table) and the ActiveConnection property (use the Set = syntax to cause this property
to point to a valid ADO Connection object). You can also omit this step and pass information
about the Source and ActiveConnection as arguments in the next step.
-
Call the Recordset's Open method. If you omitted step 3, indicate the Recordset's
Source and ActiveConnection as the Open method's first and second arguments, respectively.
Listing 8.2 illustrates the property-driven technique described in step 3 for opening a
Recordset.
LISTING 8.2
OPENING AN ADO RECORDSET BY SETTING THE ACTIVECONNECTION AND SOURCE PROPERTIES
Set cnNWind = New ADODB.Connection
Set rsEmployees = New ADODB.Recordset
Dim sConnect As String
sConnect = "Provider=Microsoft.Jet.OLEDB.3.51;"
& _
"Data Source= NWind.mdb"
cnNWind.Open sConnect
rsEmployees.Source = "Select * From Employees
Order By
LastName,FirstName"
Set rsEmployees.ActiveConnection = cnNWind
rsEmployees.Open
Listing 8.3 illustrates the use of command-line arguments to accomplish the
same result, as discussed in step 4.
LISTING 8.3
OPENING AN ADO RECORDSET WITH ARGUMENTS TO THE OPEN METHOD
Set cnNWind = New ADODB.Connection
Set rsEmployees = New ADODB.Recordset
Dim sConnect As String
sConnect = "Provider=Microsoft.Jet.OLEDB.3.51;"
& _
"Data Source= NWind.mdb"
cnNWind.Open sConnect
rsEmployees.Open _
"Select * From Employees Order By LastName,FirstName",
_
cnNWind
NOTE - Other Methods for Opening a Recordset:
The section titled "Accessing Data
with the Execute Direct Model" in the following chapter discusses how to use
the Execute method of ADO Connection and Command objects to open a Recordset.