Not all ADO Power Users realize that there are Events, specifically related to the Connection and Recordset Objects, for which they can write code for. If one is aware of these Events, it is not exactly intuitive as to how you would access them. Below, please find a listing of Events which are exposed by the ADO Recordset and Connection Objects. I will also demonstrate the mechanism by which you, the ADO programmer, can make use of these valuable tools. An in-depth discussion of these Events is well beyond the scope of this Tip. It is solely my intention to make you aware of their existence, and also to demonstrate the means by which you can utilize these critical Events.
- Events of the ADO Recordset Object:
- FetchProgress()
- FetchComplete()
- WillChangeField()
- FieldChangeComplete()
- WillMove()
- MoveComplete()
- EndOfRecordset()
- WillChangeRecord()
- RecordChangeComplete()
- WillChangeRecordset()
- RecordsetChangeComplete
- Events of the ADO Connection Object:
- WillConnect()
- ConnectComplete()
- Disconnect()
- WillExecute()
- ExecuteComplete()
- BeginTransComplete()
- CommitTransComplete()
- RollbackTransComplete()
- InfoMessage()
- How to access ADO Event Procedures. The process involved in accessing ADO Events involves what is called an Event Sink. To sink to an ADO Event, you must:
- Set a Reference to the Microsoft ActiveX Data Objects X.X Library.
- Create a Class Module, either a stand-alone Class Module or a Form's Class Module.
- Add a WithEvents variable to the Module, preferably to the Declarations Section. WithEvents is a KeyWord that specifies that an Object Variable is to be used to respond to Events triggered by an ActiveX Object. Typical WithEvents Declarations follow:
- Private WithEvents cnn As ADODB.Connection
-
Private WithEvents rst as ADODB.Recordset
- Once you add WithEvents to a Class Module, the VBA Editor adds the Event Sink Variable to the editor's drop down Object List. Once you select the Event Sink Variable from the Object drop down, the related Events to which you can respond, appear from the Procedures drop down List. To create one or more Event Procedures for that Event Sink, write code in the Procedure to react to that Event.
- Private Sub cnn_ConnectComplete(ByVal pError As ADODB.Error, adStatus As ADODB.EventStatusEnum, ByVal pConnection As ADODB.Connection)
-
'The following code segment exists in a Form's Class Module
-
'display a Connected Message and enable specific Controls.
-
'disable the Connect Button and enable an Execute Button
-
Me![lblConnect].Caption = "Connected to SQL Server Godzilla"
-
Me![cmdConnect].Enabled = False
-
Me![cmdExecute].Enabled = True
-
End Sub
- Somewhere in your code - for example in the Form's Load Event, instantiate the Event Sink Variable. This step is actually out-of-sequence with the prior step. It was logical to demo the code in the prior step:
- Set rst = New ADODB.Recordset
- Somewhere else in your code - for example in the Form's UnLoad Event, destroy the Event Sink by setting it to nothing.