We help IT Professionals succeed at work.

Access Form with labels can't cycle through labesl in vba code.  Original labels recognized but not newly added labels.

Last Modified: 2013-12-20
In the attached Access 2003 database, I created a Form and then added four labels.  I added a button and when the button is clicked the code cycles through the labels with a For Each statement (see code below).  The problem is that when I add another label, a new label, the label is not recognized when the code is cycling through the labels.  The new label has a little error message flag saying that the label is not associated with a control, I'm not sure if that matters or not.  I tried turning off the error checking for unassociated labels and it didn't fix the problem.
When the code gets to the new label it gives the error message "run time error 13 type mismatch".  If I click on Debug and continue stepping through the code, the code proceeds fine and recognizes the new label (but, only, after clicking debug, not the first time through).

I need to figure this out because I have a much larger and more complex database with lots of labels and one label accidentally got deleted and when I re-created it from new, from the toolbox, I get this same problem, I also tried copying an existing label and that didn't help, and now I can't cycle through the labels.
Option Compare Database
Private Sub Command4_Click()
Dim lblDia As Label
For Each lblDia In Me.Controls
Debug.Print lblDia.Name
MsgBox lblDia.Name
Next lblDia
End Sub

Open in new window

Watch Question

Top Expert 2016

use this codes

Dim ctl As Control
For Each ctl In Me.Controls
    If ctl.ControlType = acLabel Then
    Debug.Print ctl.Name
    MsgBox ctl.Name
    End If


Your code works for the small database that I posted but when I re-do the code in the large database, it still skips by the new label.
Top Expert 2016

do a compact and repair of your db.
tools > database utilities > compact and repair database
This one is on us!
(Get your first solution completely free - no credit card required)
capricorn1's first response worked great for me when I needed to get the names for the labels and lines on my form.  For lines, use acLine instead of acLabel..  For other types of controls that it can help with, just hit "F1" in VBA when acLine or acLabel is highlighted.  Thanks!


Scootman78,  you are right.  I am not sure that the comment at 11/12/09 02:55 PM, ID: 25809830 is correct.   I think maybe the line of code:   ctl.Parent.Name = "frmCalendario"
only associated the control temporarily.    I ended up fixing the problem with this solution:
Unlock the solution to this question.
Join our community and discover your potential

Experts Exchange is the only place where you can interact directly with leading experts in the technology field. Become a member today and access the collective knowledge of thousands of technology experts.

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.


Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.