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

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.
Code Snippet:
1:
2:
3:
4:
5:
6:
7:
8:
9:
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

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

I finally solved this by using the code from another EE solution at:
http://www.experts-exchange.com/Microsoft/Development/MS_Access/Access_Forms/Q_23187018.html

capricorn1, I used your code below to cycle through the labels, and I added some code to figure out the "parent.name" which apparently is the name of the "control" associated with the label.  This allowed me to figure out that the other similar labels were associated with the Form (named: frmCalendario"), then I used this code:   ctl.Parent.Name = "frmCalendario"
to set (or "associate"), the label with the Form.

Dim ctl As Control
For Each ctl In Me.Controls
    If ctl.ControlType = acLabel Then
    Debug.Print ctl.Name
    MsgBox ctl.Name
      If Left(ctl.Name, 9) = "lbldia132" Then
        Debug.Print ctl.Name
        Debug.Print ctl.Parent.Name
       ctl.Parent.Name = "frmCalendario"
    End If
Next
Random Solutions  
 
programming4us programming4us