ERROR in looping code

User avatar
sal21
PlatinumLounger
Posts: 4334
Joined: 26 Apr 2010, 17:36

ERROR in looping code

Post by sal21 »

Code: Select all

Private Sub TEST_HANS()

    Dim ctl As Control
    For Each ctl In Me.Controls
        If Not ctl.Container Is Nothing Then'<<<<<<< here error
            If ctl.Container.Name = "Frame2" Then
                If TypeName(ctl) = "ComboBox" And Val(ctl.Tag) >= 1 Then
                    ' Do something with ctl
                    Debug.Print ctl.Name
                End If
            End If
        End If
    Next ctl

End Sub
error on second loop
You do not have the required permissions to view the files attached to this post.

User avatar
HansV
Administrator
Posts: 78235
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: ERROR in looping code

Post by HansV »

Do you have an ImageList or CommonDialog or Timer control on the form?
Best wishes,
Hans

User avatar
sal21
PlatinumLounger
Posts: 4334
Joined: 26 Apr 2010, 17:36

Re: ERROR in looping code

Post by sal21 »

HansV wrote:
17 Dec 2021, 13:08
Do you have an ImageList or CommonDialog or Timer control on the form?
only two timer

User avatar
HansV
Administrator
Posts: 78235
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: ERROR in looping code

Post by HansV »

Timers don't have a Container. That causes the error. Try this:

Code: Select all

Private Sub TEST_SAL21()
    Dim ctl As Control
    For Each ctl In Me.Controls
        If TypeName(ctl) = "ComboBox" Then
            If Not ctl.Container Is Nothing Then
                If ctl.Container.Name = "Frame2" Then
                    If Val(ctl.Tag) >= 1 Then
                        ' Do something with ctl
                        Debug.Print ctl.Name
                    End If
                End If
            End If
        End If
    Next ctl
End Sub
Best wishes,
Hans

User avatar
sal21
PlatinumLounger
Posts: 4334
Joined: 26 Apr 2010, 17:36

Re: ERROR in looping code

Post by sal21 »

HansV wrote:
17 Dec 2021, 15:05
Timers don't have a Container. That causes the error. Try this:

Code: Select all

Private Sub TEST_SAL21()
    Dim ctl As Control
    For Each ctl In Me.Controls
        If TypeName(ctl) = "ComboBox" Then
            If Not ctl.Container Is Nothing Then
                If ctl.Container.Name = "Frame2" Then
                    If Val(ctl.Tag) >= 1 Then
                        ' Do something with ctl
                        Debug.Print ctl.Name
                    End If
                End If
            End If
        End If
    Next ctl
End Sub
GENIUS!
now work!