Callback Order of Precedence

Callbacks in the Composite Controls Object Model are called in reverse order in which they are added to an event. That is, if one were to add Callback1, Callback2, and Callback 3 sequentially to a cControl_EventHandler event, when the event fires, the callbacks will be executed in reverse order, beginning with Callback3. The only override to the order of precedence is to specify the boolean flag bAddlast as True in the AddCallbacks() method of the cControlChild_Event class. Doing so will add the callback to the end of the delegate collection, causing it to be called last.

The other aspect of callback order of precedence is manifest in the OnStartup / OnTerminate events that are standard to Composite Controls. One feature of the object model is the ability to attach a control (like a TextBox) to it's parent (a Form or Report). This attachment is managed by attaching the control's OnStartup / OnTerminate events to the parent form / report's OnStartup / OnTerminate events. The events at the form / report level are attached to the OnLoad / OnClose events of the form / report, respectively.

Using the example of a form containing a text box, the order of event firing for the OnStartup and OnTerminate events are as follows:

Form_Load() --> cControl_EventHandler_Form.OnStartup() --> cControl_EventHandler_TextBox.OnStartup()
Form_Close() --> cControl_EventHandler_TextBox.OnTerminate() --> cControl_EventHandler_Form.OnTerminate()

Note that the control's OnStartup event is fired after the form's OnStartup event, and fired before the form's OnTerminate event.

This affects order of precedence as follows:
  • Callbacks assigned to the textbox EventHandler's OnStartup event are always fired after callbacks assigned to the form EventHandler's OnStartup event.
  • Callbacks assigned to the textbox EventHandler's OnTerminate event are always fired before callbacks assigned to the form EventHandler's OnTerminate event.

Demonstrate callback order of precedence

Very quickly, we can demonstrate the principles outlined above by adding the following code to the end of the Form_Open() event:

    With cmdHandler
         .OnStartup.AddCallback callbacks, "Callback1", ccFullSignature
         .OnTerminate.AddCallback callbacks, "Callback1", ccFullSignature
        With .ParentEventHandler
            .Events("OnStartup").AddCallback callbacks, "Callback1", ccFullSignature
            .Events("OnTerminate").AddCallback callbacks, "Callback1", ccFullSignature
        End With
    End With

The above code adds the callback function, "Callback1" to the OnStartup / OnTerminate events for the form and command button. Add the code, save changes, and run the Autoexec macro to see callback order of precedence between the form and the command button.

To demonstrate local precedence at the control level, add the line:

.OnStartup.AddCallback, callbacks, "Callback2"

immediately before the statement,

With .ParentEventHandler

Because "Callback2" is added last to the command button's OnStartup Event, it will execute before "Callback1", but not before the callback function assigned to the form-level OnStartup event.

Return to Tutorials Main Page

Last edited Feb 26, 2011 at 7:16 AM by vba_junkie, version 4


No comments yet.