Now let's take a look at the methods available on the Command object.
Command Object Methods | |
---|---|
Method Name | Description |
Cancel | Cancels an asynchronous query |
CreateParameter | Creates a Parameter object for the Command object's Parameters collection |
Execute | Executes your query |
The Cancel method allows you to terminate the execution of an asynchronous query. Keep in mind that if you execute the query using the asynchronous fetching option, canceling the query after the ExecuteComplete event of the Connection object has fired will have no effect.
The CreateParameter method returns a Parameter object. If you're using a parameterized query, you can populate the Parameters collection for the Command object by using the Append method on the Parameters collection combined with the CreateParameter method on the Command object. Here are the parameters that the CreateParameter method supports:
The parameters on the CreateParameter method correspond to most of the properties on the Parameter object that you're liable to set. I'll describe these parameters in more detail later in the chapter when we cover the Parameter object. Perhaps the only properties you might need to set on the Parameter object that aren't available as parameters on the CreateParameter method are the NumericScale and Precision properties.
As its name implies, the Execute method executes the query your Command object contains. By default, this method will return a Recordset object with the results of your query. It has the following parameters:
The initial parameter, RecordsAffected, will contain a long integer that indicates the number of records affected by your query. If you're using action queries to update data in your database, you'll probably want to know how many records your query modified.
The Execute method also takes a parameter, Parameters, that you can use to supply values for the parameters of your query without having to use the Parameters collection. This parameter on the Execute method accepts a Variant array of values and/or variables that correspond to the items in your Command object's Parameters collection. Values supplied in Parameters will override the values in the Parameters collection.
Just as with the Options parameter of the Recordset.Open and Connection.Execute methods, you can use this final parameter of the Execute method with constants from CommandTypeEnum and/or ExecuteOptionEnum (see Chapter 4). The parameter is a bitmask, so you can combine appropriate constants such as adCmdText + adExecuteNoRecords. You can also specify a value or values from ExecuteOptionEnum in this parameter to execute your command asynchronously.