Color scheme: Light
You are here: start » en » Documentation » Microsoft Access development tools » WKL Forms Object Library » WKL Forms Object Library how-to guides » How to: Create event procedures for a control

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):

ControlDesigner Events page (EventProcedures)Object and procedure lists in the code window
WklTreeView, WklTreeList, WklGantt, WklGrid, WklPropertyGridYesYes
WklSplitter, WklDockLayout, WklStatusLabel, WklNavBar, WklButton, WklValidator, WklToggleSwitch, WklTokenBox, WklBadgeNoYes
WklToast, WklLinkManager (objects, not controls)NoOnly 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):

  1. Select the control in Design view, and go to the Other tab of the property sheet.
  2. Click [...] in the EventProcedures row. The Events page lists every event with a short description and marks the ones that already have a procedure.
  3. Select the event (the search box at the top filters the list), and click Create / open procedure, or double-click the event.
  4. 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 EventProcedures row 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.
  • NodeFormat and CellFormat occur only with FormatEvents = True (troubleshooting).
  • 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 WklUpdateAction requires a reference to the WKL Forms Object Library (Tools › References). Access sets it when you insert the control by hand; a form loaded with LoadFromText doesn't get it, and without it, even the correct declaration doesn't compile. Long in place of the enumeration compiles with or without the reference.
    • A module that doesn't compile isn't reported anywhere: RunCommand acCmdCompileAndSaveAllModules raises no error, and DoCmd.OpenForm called through Automation returns without opening the form. Check Application.IsCompiled after compiling. The designer page and the code window lists always write the declaration correctly.
en/wiki/msaccess/comctl/how-to/create-event-procedures.txt · Last modified: by Wolfgang van der Stille