RDC00407 — Unmatched event handler
Severity: Hint · Family: RDC (Rubberduck Core analyzers) ·
Raised by: UnmatchedEventHandlerAnalyzer
A procedure of a form is named as the handler of an event of one of its controls, and does not match the event: nothing ever calls it.
A procedure named ControlName_EventName handles that event of the form's control (TextBox1_Change handles the Change event of TextBox1), when it is a
Sub that takes the event's parameters. One that is a Function, or that takes other parameters, is not called when the event is raised. MS-VBA compiles
it all the same, and says nothing about it (recorded in Excel): it is dead code. The editor shows it the way it shows code that cannot be reached.
A Private procedure like that which the form's own code calls is not dead, and is not reported: it is an ordinary procedure, with a misleading name. A
Public one is not reported either, because code elsewhere in the project could call it through the form.
How it is raised
| Situation | Example, for a form with a TextBox1 |
Resolution |
|---|---|---|
| A handler of a control's event, with parameters the event does not have. | Private Sub TextBox1_Change(ByVal Value As Long) |
Give it the event's parameters (Private Sub TextBox1_Change()), or remove it. |
A handler of a control's event declared as a Function. |
Private Function TextBox1_Change() As Long |
Declare it a Sub, or remove it. |
The quickest way to write a handler that matches is to let the editor write it: pick the control and the event, and the procedure is declared with the event's parameters.
Notes
For a WithEvents variable that the code declares, rather than a form's control, a handler that does not match its event is a compile error,
VBC09326 Invalid event handler, in MS-VBA as in RD-VBA.
See also
- RD-VBAL §2.6.4 Rubberduck Core Diagnostics
- RD-VBAL §5.3.1.8 Event Handler Declarations
- VBC09372 Member already exists in an object module from which this object module derives
ACCUEIL • HOME | ℹ️ BIENVENUE • WELCOME | 🧩 BÂTISSONS • BUILD | RD-VBAL | SDK | 🌐 rubberduckvba.ca