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  
  •  Do you know how I can prevent the Run time error '3010'  table 'FA_ledger_report' already exists ? Actually the 'FA_ledger_report' is a Union query.
  •  How to Take ownership of folder and all subdirectories and files?
  •  How to prevent the auto update within an Acces 2007 template
  •  Using Server.Mappath to get a file from a virtual directory
  •  What is the MS Access 2007 version of this code?
  •  How to clear @@error or general error flag in T-SQL
  •  Windows 2008: When they say "CPUs supported", are they talking CPU sockets or CPU cores?
  •  Relational Database Cardinality (How is it expressed)
  •  Error "windows cannot access the specified device path, or file. you may not have the appropriate permissions to access them"
  •  Where and how can i use silent switches for a MSI file in the group policy.
  •  
    programming4us programming4us