Tutorial 1, Part I - Create the Event Handler

Create the cControl_EventHandler class for the CommandButton object.


Step 1 - Create the cControl_EventHandler_CommandButton class

Step 2 - Other related code changes

Step 3 - Add the Form-level code

Step 1 - Create a command button cControl_EventHandler class

1. Right-click in the Project Explorer window at the upper left and from the menu, select Insert -> Class Module.
2. Rename the class module (Name in the Properties window in the lower left) to cControl_EventHandler_CommandButton.
3. Open the cControl_EventHandler_TEMPLATE class module, press CTRL+A to select everything, then press CTRL+C to copy,
4. Open the _cControl_EventHandler_CommandButton class module you just created. Type CTRL+A to select any existing code and press CTRL+V to paste the code.
5. Alter the code of the class as follows:
A. Highlight the word ControlType_ in the line
Private WithEvents o As ControlType_
and press CTRL+H.
B. In the Replace With dropdown, type "CommandButton". Make sure the Current Module option is selected in the Search part of the dialog.
C. Click Replace All. You should get a dialog indicating 5 replacements were made. Click OK and close the dialog.
D. Scroll down to near the end of the declarations section to the code:

Public OnStartup As cControlChild_Event 
Public OnTerminate As cControlChild_Event
Public OnEvent As cControlChild_Event

E. Highlight OnEvent and press CTRL+H.
F. Type "OnClick" in the Replace With drop down. Click Replace All. Eight replacements should be made.
{NOTE: This change affected the event placeholder *OnEvent*. To add more events, you'll need to add an extra cControlChild_Event declaration in the declarations section, initialize it with InitEvents() in Initialize(), and terminate the event object in Terminate().}
G. Add an event trap routine. There are two ways to do this:
I. Select o from the dropdown at the top left of the VBA code window. The Click event will be added automatically.
II. Add the code manually.
{NOTE: For accuracy option I above is recommended. However, the code will be inserted somewhat randomly in the module and you will have to copy / paste it elsewhere if you wish. To add further events automatically, choose the event name from the top right dropdown.}
H. After adding the event stub, add the following code to the body:

Private Sub o_Click()

      If (OnClick.IsActive) Then

            RunCallbacks m_oRef, OnClick

      End If

End Sub

{NOTE: Adding more events will require adding stubs with similar code as outlined above.}


Step 2. Other related code changes

1. Open the Support_CompositeControlManager module
2. Scroll down to the GetHandler() function (press CTRL + Down Arrow to move function-by-function).
3. Uncomment the following code:
Case ccControl_EventHandler_CommandButton

        Set GetHandler = New cControl_EventHandler_CommandButton

{NOTE: Do not uncomment handler code for classes that do not exist in the project. Doing so will generate an error.}


Step 3. Add the Form-level code

1. Open the module for the StartUp form. Add Option Explicit to the declarations section.
2. Add the Form_Open Event.
{NOTE: IInitialize Composite Controls in a form or report's Open event. That way, a Form or Report's Load event (which immediately follows) may be trapped.}
3. In the Form_Open procedure, add the following code:
Private Sub Form_Open(Cancel As Integer)

    CreateEventHandler CreateGenericReference(Me.Command0, ccCommandButton)

End Sub

{NOTE: In order to pass an ActiveX or other event-sourcing object to a Composite Control, it must be wrapped in a cControl_Generic object. The function CreateGenericReference() does this, provided a valid reference and ccControlType enumeration.}).

4. Now switch to the StartUp form (which should be open in Design View). Move the Cancel button to the middle of the dialog, and edit the caption to read "Test" or something else. Do not change the name ("Command0"), or you will have to change the Me.Command0 reference in the Form_Open() event procedure.

5. Close the form, (be sure to save it) and run the Autoexec macro. The StartUp dialog should open with the "OK" button in the lower right and the "Test" button in the middle. Click on the "Test" button. Nothing should happen. Close the form with OK or the X in the upper-right.


Now move on to the next part, Create the Callback Class

Last edited Feb 6, 2011 at 6:10 PM by vba_junkie, version 9


No comments yet.