Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
622 views
in Technique[技术] by (71.8m points)

excel - ExecuteComplete ADODB Connection event not fired with adAsyncExecute parameter

I have a problem trying to catch the completion of a stored proc execute asynchronously.

Below my code VBA (in a class module named clsAsync):

Option Explicit

Private WithEvents cnn As ADODB.Connection


Private Sub cnn_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)
    MsgBox "Execution completed"
End Sub

Sub execSPAsync()
    Set cnn = New ADODB.Connection
    Set rst = New ADODB.Recordset
    cnn.ConnectionString = "connection to my database SQLSEREVER"
    cnn.Open
    cnn.Execute "kp.sp_WaitFor", adExecuteNoRecords, adAsyncExecute
End Sub

This class is PublicNotCreatable.

To call the sub execSPAsync from a module I use the following code:

Sub testASYNC()
    Dim a As New clsAsync
    Call a.execSPAsync
End Sub

The stored procedure is very simple:

alter PROC kp.sp_WaitFor
AS

WAITFOR DELAY '00:00:05'

My problem is that the event ExecuteComplete is not fired at all, while if I comment the adAsynExecute parameter all is working fine. Any idea on how to solve my question?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

I solved my problem replacing the calling code:

Sub testASYNC()
    Dim a As New clsAsync
    Call a.execSPAsync
End Sub

with this new code:

Private a As clsAsync

Sub testASYNC()
    Set a = New clsAsync
    Call a.execSPAsync
End Sub

In the async mode, the object "a" is no longer available at the end of the procedure (scope visibility issue).


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...