Page 509 - Microsoft Office Excel 2003 Programming Inside Out
P. 509
Introducing ADO
Using the ADO Object Model
There are two basic scenarios for using ADO depending on the type of command you want to
execute. If the command doesn’t return any rows, the following scenario works well.
1 Create a Connection object to connect to the desired database.
2 Open the Connection object.
3 Prepare a Command object that contains the database command to be executed.
4 Add any necessary Parameter objects to the Command object, including the values to
be passed to the command. Remember that not all commands require parameters.
5 Set the ActiveConnection property to the Connection object.
6 Execute the Command object.
7 Close the Connection object.
If the command returns rows from the database, you should use this approach:
1 Create a Connection object to connect to the desired database.
2 Open the Connection object.
3 Prepare a Command object that contains the database command to be executed.
4 Add any necessary Parameter objects to the Command object, if the Command object
has parameters.
5 Set the ActiveConnection property to the Connection object.
6 Create a new Recordset object with all the options that you plan to use.
7 Set the ActiveCommand property in the Recordset to the Command object that
you created.
8 Open the Recordset object.
9 Retrieve the rows from the Recordset object.
10 Close the Recordset object.
11 Close the Connection object.
The Connection Object Chapter 23
The Connection object describes the connection between the application program and the
database server. Coupled with the Errors collection and the Error object, the Connection
object controls the logical connection to the database.
483
Part 6: Excel and the Outside World: Collaborating Made Easy

