Turning Events Off

In addition to canceling operations by altering the status parameter of an event, you can also turn the events off completely for an open instance of an object. By setting any event’s status parameter to adStatusUnwantedEvent, you are informing ADO that you no longer want to be notified of a particular event.

To illustrate this ability, alter the WillMove event as shown:

Private Sub rst_WillMove(ByVal adReason As ADODB.EventReasonEnum, _
                         adStatus As ADODB.EventStatusEnum, _
                         ByVal pRecordset As ADODB.Recordset)
                         
    Debug.Print
    Debug.Print "  WillMove Event Raised"
    adStatus = adStatusUnwantedEvent
    Debug.Print "  Turned WillMove Event Off"

End Sub

Now enter and execute a piece of code that raises the WillMove event at least once:

Debug.Print "--------- Before Opening Recordset"
rst.Open "Authors", _
         "DSN=BiblioDSN"
         
Debug.Print
Debug.Print "--------- Before MoveFirst"
rst.MoveFirst

Debug.Print
Debug.Print "--------- Before MoveNext"
rst.MoveNext

Debug.Print
Debug.Print "--------- Before Closing Recordset"
rst.Close

When this code is executed, the following output is sent to the Immediate Window:

--------- Before Opening Recordset

  WillMove Event Raised
  Turned WillMove Event Off

--------- Before MoveFirst

--------- Before MoveNext

--------- Before Closing Recordset

Note that once a Recordset object (or a Connection object for that matter) is closed and then reopened, the event will be raised again.

In the case of events that can be raised for more than one reason, you must indicate ...

Get ADO: ActiveX Data Objects now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.