MDAC 2.5 SDK - Technical Articles


 

Asynchronously Executing a Command

Execution is another operation that can take a significant amount of time. ADO allows asynchronous execution in two ways: using the Connection.Execute method and using the Command.Execute method.

Asynchronous execution covers a particular period of time. That is, it covers the time from when the data source object is requested to begin an operation to the time that the first row is available to be fetched (assuming a row-returning result).

The code for asynchronous execution is similar to the code for asynchronous connection. The Options parameter to the ADO execute methods must include the value adAsyncExecute. However, because most options parameters take more than one value, adAsyncExecute could be joined with another option by using an OR operation.

In the following example, an event handler has been implemented to print to the Debug window when the command has completed.

Dim WithEvents conn As ADODB.Connection

Sub Form_Load()

Dim cmd As New ADODB.Command
Set conn = New ADODB.Connection

conn.ConnectionString = _
"Provider=SQLOLEDB;Data Source=sql70server;" _
   & "User ID=sa;Password='';Initial Catalog=pubs"
conn.Open

Set cmd.ActiveConnection = conn
cmd.Execute "select * from authors", , adAsyncExecute
Debug.Print "Command Execution Started."

End Sub

Private Sub conn_ExecuteComplete(ByVal RecordsAffected As Long, ByVal pError As ADODB.Error, adStatus As ADODB.EventStatusEnum, ByVal pCommand As ADODB.Command, ByVal pRecordset As ADODB.Recordset, ByVal pConnection As ADODB.Connection)

Debug.Print "Completed Executing the Command."

End Sub