logo
search
VBA & Macro Problems

How to Identify a Clicked Label in Microsoft Access VBA

Ayan MasoodAyan Masood Sep 25, 2026 868 views

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.

How to Identify a Clicked Label in Microsoft Access VBA
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 you start

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.

Solution 1Recommended

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.

1
Create a New Class Module

Open the VBA Editor (ALT + F11). Go to Insert > Class Module. Name the new class module 'clsLabelHandler' in the properties window.

2
Declare the WithEvents Variable

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.

3
Instantiate the Class in Your Form

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.

Use a Class Module for Reusable Label Events
Best Practice: Make sure to set the 'On Click' property of the labels to '[Event Procedure]' during the initialization loop, otherwise the class module will not catch the event.
Free Microsoft Office alternative

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. 1. Download WPS Office: Visit the official WPS Office website and download the free installer for your operating system.
  2. 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. 3. Save and Share: Save your macro-enabled workbooks in the standard .xlsm format to maintain full compatibility with Microsoft Excel.
Free and lightweight Office suite with lightning-fast installationHigh compatibility with Microsoft Office file formats (.xlsx, .docx, .pptx)Advanced VBA and macro environment in WPS SpreadsheetFamiliar user interface ensuring zero learning curve and smooth migration
microsoft office alternative - wps office

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.