Deep Dive: Improving the Access User Experience with Attached Labels
In this comprehensive article, we explore attached labels in extensive depth: what they are, why you want them, and how to work with them in both the UI and VBA.
Finishing touches help distinguish a professionally developed Access application.
One of those finishing touches is to ensure all labels are attached to their associated controls (text boxes, combo boxes, etc.) wherever appropriate. Will your application function without this feature? Sure. But associating your labels with relevant edit controls will help take your application to the next level.
What's an Attached Label
In Microsoft Access, an "attached label" in the context of a form refers to a text label that is associated with a form control, such as a text box, combo box, list box, or other data-entry components.
The attached label typically contains descriptive text that indicates the purpose or contents of the associated control to the user. For example, next to a text box where the user is expected to enter their first name, the attached label might read "First Name."
When you create a new control on a form using the Access form designer, you often have the option to add a label with it. If you use the wizard or drag and drop fields from the field list, Access automatically creates attached labels for these controls.
The Benefits of Attached Labels
- Larger Mouse Target Area
- Improved Keyboard Efficiency
- Simplified Form Design
Larger Mouse Target Area
When you click on an attached label, Access sets focus to the associated control. This effectively increases the "mouse target area" of the edit control (e.g., the text box or combo box) to include both the control and its associated label.
Additionally, clicking on the label will select the entire value in the associated control. When you click directly on the text of a text box or combo box, the cursor gets placed at that point in the string and no text is selected. That's an efficient way to change a typo in a text box, but it's less efficient if you want to change the entire value.
Improved Keyboard Efficiency
By including an ampersand in the label caption, you automatically create an [Alt] shortcut key for the associated control.
For example, assume you have a First Name text box on your form. If you set the caption for the associated label to &First Name:
then the label will appear like this on the form:
First Name: [_______________]
If the user presses [Alt] + [F], then Access will set the focus to the associated text box.
The ampersand does not have to go in the front of the caption. For example, you could have a last name text box with a label whose caption is Last &Name:
. In that case, the "N" will be underlined and the associated shortcut for the control will be [Alt] + [N]:
Last Name: [_________________]
You will want to be careful when configuring these shortcuts that you don't accidentally create duplicate shortcut keys. It won't generate errors, but it will be confusing for your users.
Simplified Form Design
When a label is attached to another control like a text or combo box, the label and its associated control move together in design view:
- If you move the label, its text box moves.
- If you move the text box, its label moves.
NOTE: If you want to move the label or text box independently, you can click on the little gray box in the upper-left corner of the label or control.
Identifying Unmatched Labels
It used to be very difficult to identify unmatched labels in Access.
Newer versions, however, display a green triangle in the corner of the label if it is not attached to another control.
If you select the control, an exclamation point warning icon will appear to the left of the control. If you hover over this icon, you will see the following message:
This is a new label and is not associated with a control.
If you click the drop-down arrow next to the icon, it will identify the error as a "New Unassociated Label." You will also see a series of commands you can use to address the error:
- Associate Label with a Control...: pops up a dialog to choose an associated control (see screenshot below)
- Help on This Error: shows you in-app help for unassociated labels (see screenshot below)
- Ignore Error: ignores this one unassociated label (there are often good reasons to have unassociated labels, such as for large header text)
- Error Checking Options...: this will take you to the "Object Designers" Options page which will allow you to turn off error checking for unassociated labels (see below for details)
Associate Label Dialog Window
Here's the window that pops up when you choose "Associate Label with a Control..." from the warning icon dropdown:
In-App Help Screenshot
Here's the help screen that appears when you click on the "Help on This Error" option via the warning icon dropdown:
Error Checking Options
There are two similar error checks related to unassociated controls. If you find these green triangle error identifiers annoying (they do generate false positives since some unassociated labels are intentional and proper), then you can turn them off by unchecking them in the "Object Designers" page of the Access options.
To access this dialog, use the error drop-down as described above, or go to File > Options > Object Designers and scroll to the bottom of the list of options:
Methods to Attach Label
- Via the "Associate Label" dialog as shown above
- Auto-added when adding another control (if its default "Auto Label" property is set to Yes)
- Select label > Cut > Select associated control > Paste
- Enter the label's name in the associated control's "Label Name" property (available in the "Other" tab of the control's Property Sheet)
- In VBA, like so:
Me.tbCustomer.Properties("LabelName").Value = "lblCustomer"
NOTE: To detach the control in VBA, set the "LabelName" value to an empty string: Me.tbCustomer.Properties("LabelName").Value = ""
.
Accessing the Attached Label in VBA
If a control has an attached label, it will be the first item in the control's .Controls
property:
Debug.Print "Name of customer text box's associated label control:"
Debug.Print Me.tbCustomer.Controls(0).Name
In newer versions of Access (I don't know when it first appeared), the name of the attached label control will also be available via the deeply hidden "LabelName" property:
Debug.Print "Name of customer text box's associated label control:"
Debug.Print Me.tbCustomer.Properties("LabelName").Value
If a label is attached to a control, the associated control will be the object in the label's .Parent
property:
Debug.Print "Name of customer label's associated text box control:"
Debug.Print Me.lblCustomer.Parent.Name
To check if the control has an associated label, check the count of its .Controls
property:
Debug.Print "True or False: The customer text box has an associated label:"
Debug.Print (Me.tbCustomer.Controls.Count > 0)
To check if a label is attached to a text box, we can use the TypeOf operator against the label's .Parent
property:
Debug.Print "True or False: The label is associated with a text box control:"
Debug.Print (TypeOf Me.lblCustomer.Parent Is Textbox)
Checking if a label is associated with any arbitrary control is a bit trickier. We can't simply check to see if the label's .Parent
property is Nothing
, because an unassociated label will still have a parent: it will be either the Form
or Report
object (depending on whether the label is on a form or report). Thus, to check if the label is unassociated, we would need to do something like this:
Debug.Print "True or False: The label is associated with an edit control:"
Debug.Print Not ((TypeOf Me.lblCustomer.Parent Is Form) Or (TypeOf Me.lblCustomer.Parent Is Report))
The above code should work in most cases, but there may be situations where a label has a parent that is not its associated control OR its parent form OR its parent report. Off the top of my head, the above code may not work if the unattached label is part of a tab control or possibly an option group control. Be aware that you may need to adjust the above condition based on your unique circumstances.