How to: Create event procedures for a control
Handle an event of a WKL control in the form module, such as the NodeSelect event of
WklTreeView, ValidateCell of WklGrid, or Click of WklButton.
Requires WKL Forms Object Library 1.23 or later.
Solution overview
Access never lists an ActiveX control's events on the Event tab of the property sheet. You
create event procedures with the object and procedure lists in the code window, which works for
every WKL control. For the tree and grid controls, you can also use the designer's Events page
(the EventProcedures row in the property sheet):
| Control | Designer Events page (EventProcedures) | Object and procedure lists in the code window |
|---|---|---|
WklTreeView, WklTreeList, WklGantt, WklGrid, WklPropertyGrid | Yes | Yes |
WklSplitter, WklDockLayout, WklStatusLabel, WklNavBar, WklButton, WklValidator, WklToggleSwitch, WklTokenBox, WklBadge | No | Yes |
WklToast, WklLinkManager (objects, not controls) | No | Only for a WithEvents variable at the top of a class or form module |
The form must have a module (Has Module = Yes); a form without a module doesn't receive events from any control.
Implementation
With the designer (tree and grid controls):
- Select the control in Design view, and go to the Other tab of the property sheet.
- Click
[...]in theEventProceduresrow. The Events page lists every event with a short description and marks the ones that already have a procedure. - Select the event (the search box at the top filters the list), and click Create / open procedure, or double-click the event.
- Close the page with OK. Access creates the procedure and opens the code window there.
With the code window (every control): open the form module (View Code), choose the control
(for example, Tree1) in the object list, and choose the event in the procedure list. For an object
such as WklToast, declare the variable at module level first; it then appears in the object list:
Private WithEvents mToast As WklToast ' at the top of the module, not inside a procedure
Private Sub Form_Load()
Set mToast = New WklToast
End Sub
The procedure gets the signature from the type library:
Private Sub Tree1_NodeDrop(ByVal SourceKey As Variant, ByVal TargetKey As Variant, _
ByVal Position As WklDropPosition, ByVal Effect As WklDropEffect, Cancel As Integer)
If MsgBox("Move this node?", vbYesNo) = vbNo Then Cancel = True
End Sub
Remarks
- The procedure is created only after you click OK; Cancel discards the request. Access can't be called back while the property frame is open.
- The
EventProceduresrow shows how many events have a procedure; typing in it has no effect. - Events report user actions; the same operations called from code don't raise events.
- A procedure you type by hand must have the event's parameters exactly as the API reference lists them, in number and order, for example
treMain_BeforeUpdate(ByVal Key As Variant, ByVal Action As WklUpdateAction, Cancel As Integer). Otherwise:- A declaration with a different number of parameters, such as
BeforeUpdate(ByVal Action As Long), prevents the whole form module from compiling, with or without a reference to the type library. - An enumeration type such as
WklUpdateActionrequires a reference to the WKL Forms Object Library (Tools › References). Access sets it when you insert the control by hand; a form loaded withLoadFromTextdoesn't get it, and without it, even the correct declaration doesn't compile.Longin place of the enumeration compiles with or without the reference. - A module that doesn't compile isn't reported anywhere:
RunCommand acCmdCompileAndSaveAllModulesraises no error, andDoCmd.OpenFormcalled through Automation returns without opening the form. CheckApplication.IsCompiledafter compiling. The designer page and the code window lists always write the declaration correctly.
Related topics
- Each control's events: the “Events” section of its page in the API reference