How to Identify a Clicked Label in Microsoft Access VBA
Question details
The user wants to create a reusable VBA script to identify which label was clicked in Microsoft Access, avoiding the need to write separate code for every individual label.

- Product
- Microsoft Access
- Device & OS
- not provided
- Scenario
- Developing an Access form with multiple labels and trying to streamline the VBA codebase by handling label click events dynamically.
- Observed behavior
- Currently, individual event handlers must be written for each label. The goal is to detect the clicked control using a unified approach.
Before modifying your database, ensure you have backed up your Access file and that your macro security settings are configured to allow VBA code execution.
Use a Class Module for Reusable Label Events
Implement a custom class module using the WithEvents keyword to handle clicks for multiple labels dynamically, eliminating redundant code.
Since Microsoft Access does not support native control arrays, using a Class Module is the most robust way to assign a single event handler to multiple controls of the same type.
Open the VBA Editor (ALT + F11). Go to Insert > Class Module. Name the new class module 'clsLabelHandler' in the properties window.
Inside the class module, declare a public variable with events: 'Public WithEvents Lbl As Access.Label'. Then, create a 'Lbl_Click()' event procedure within the class to define what happens when the label is clicked.
In your Form's code module, declare a module-level Collection variable. In the Form_Load event, loop through the form's controls. If the control type is acLabel, create a new instance of your class, assign the control to the class's Lbl property, and add the instance to your collection.

Handle Events via the Associated Text Box
Leverage the default Access behavior where clicking an attached label automatically transfers focus to its associated text box.
Utilize the Access HitTest Method
Capture mouse coordinates globally and determine which control is positioned underneath the cursor using the HitTest technique.
Looking for a Lightweight Office Suite with Macro Support?
While Microsoft Access handles complex database management, WPS Office provides a highly compatible and lightweight alternative for your daily spreadsheet, document, and presentation needs. WPS Spreadsheet offers excellent VBA macro support, allowing you to automate tasks seamlessly without expensive subscription costs.
- 1. Download WPS Office: Visit the official WPS Office website and download the free installer for your operating system.
- 2. Enable VBA Macros: Open WPS Spreadsheet, go to the Developer tab, and access the VBA Editor to start writing and running your automation scripts.
- 3. Save and Share: Save your macro-enabled workbooks in the standard .xlsm format to maintain full compatibility with Microsoft Excel.

Frequently Asked Questions
Why doesn't Microsoft Access VBA support control arrays like older Visual Basic?
Microsoft designed Access forms differently from standard VB6 forms. Control arrays were omitted in favor of VBA collections and Class Modules, requiring developers to use the WithEvents keyword to handle dynamic control events.
What does Screen.ActiveControl do in Access VBA?
Screen.ActiveControl is a global property that returns the control object that currently has the focus. It is useful for determining which text box or button a user just interacted with, though it cannot be used directly on labels because labels cannot receive focus.
Can I use a public function instead of an Event Procedure for multiple labels?
Yes. In the Property Sheet for the labels, you can set the On Click property to '=MyCustomFunction()'. Inside that function, you can use Screen.ActiveControl or pass specific parameters to determine which label triggered the action.




