ADO events not firing.

romanemul

New Member
Joined
Jul 22, 2016
Messages
35
Hello boys,

This is my code placed in a Workbook module. But the events do not fire. Can anyone know what is going on ?

Code:
Private WithEvents dbConnection As ADODB.Connection
Private WithEvents dbrecordset As ADODB.Recordset


Private Sub dbConnection_ConnectComplete(ByVal pError As ADODB.Error, adStatus As ADODB.EventStatusEnum, ByVal pConnection As ADODB.Connection)
        
        MsgBox "connection complete" & adStatus & ""
End Sub


Private Sub dbconnection_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 "Finished"
    
End Sub


Private Sub dbconnection_InfoMessage(ByVal pError As ADODB.Error, adStatus As ADODB.EventStatusEnum, ByVal pConnection As ADODB.Connection)


    MsgBox "You are not connected" & adStatus & " & pError & "


End Sub


Private Sub dbConnection_WillExecute(Source As String, CursorType As ADODB.CursorTypeEnum, LockType As ADODB.LockTypeEnum, Options As Long, adStatus As ADODB.EventStatusEnum, ByVal pCommand As ADODB.Command, ByVal pRecordset As ADODB.Recordset, ByVal pConnection As ADODB.Connection)
    MsgBox "yes"
End Sub


Private Sub dbrecordset_FetchComplete(ByVal pError As ADODB.Error, adStatus As ADODB.EventStatusEnum, ByVal pRecordset As ADODB.Recordset)
   
 MsgBox "F Complete"

End Sub


Private Sub dbrecordset_WillChangeField(ByVal cFields As Long, ByVal Fields As Variant, adStatus As ADODB.EventStatusEnum, ByVal pRecordset As ADODB.Recordset)

   'somecode
    
End Sub


Private Sub Workbook_Open()
    
    Dim dbConnection As ADODB.Connection
    Dim dbCommand As ADODB.Command
    Dim dbrecordset As ADODB.Recordset
    
If dbConnection Is Nothing Then
    
    Set dbConnection = New ADODB.Connection
    Set dbCommand = New ADODB.Command
    Set dbrecordset = New ADODB.Recordset
        
        dbConnection.ConnectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=somefile.accdb;Persist Security Info=False;"
        dbConnection.Open (dbConnection.ConnectionString)
        dbCommand.ActiveConnection = dbConnection
        dbCommand.CommandType = adCmdStoredProc
        Set dbrecordset = dbConnection.Execute("SELECT first_name FROM Record")
        
Else
    
    MsgBox "Already connected"
            
End If
    
    
End Sub


Help will be appreciated.

Roman
 

Excel Facts

Excel Joke
Why can't spreadsheets drive cars? They crash too often!
Remove these two lines from the Workbook_Open code:

Code:
    Dim dbConnection As ADODB.Connection
    Dim dbrecordset As ADODB.Recordset
 
Upvote 0
Thanks RoryA. You have saved me once again.

So the Withevents keyword has the same effect as dim statement ?
 
Upvote 0
No - Private works the same way as Dim here. WithEvents specifies that you want to be able to monitor the object's events.
 
Upvote 0

Forum statistics

Threads
1,223,958
Messages
6,175,629
Members
452,661
Latest member
Nonhle

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top