loop / iterate label in frame

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

loop / iterate label in frame

Post by sal21 »

Possible to loop/iterate in Frame1 (in a vba userform) only all label?

in effect i have various label in frame1 and i need only this label, not the rest in userform

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

Re: loop / iterate label in frame

Post by HansV »

For example:

Code: Select all

    Dim ctl As Control
    For Each ctl In Me.Frame1.Controls
        If TypeName(ctl) = "Label" Then
            ' Do something with the control
            Debug.Print ctl.Name, ctl.Caption
        End If
    Next ctl
Best wishes,
Hans

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

Re: loop / iterate label in frame

Post by sal21 »

HansV wrote:For example:

Code: Select all

    Dim ctl As Control
    For Each ctl In Me.Frame1.Controls
        If TypeName(ctl) = "Label" Then
            ' Do something with the control
            Debug.Print ctl.Name, ctl.Caption
        End If
    Next ctl
OPS...

I need to iterate only to all label named:

B..
BB..
BBB...

my test code, i need to change the forecolr in vbred only to the label Left(TEST, 1) = "B":

Code: Select all

Dim CTL As Control
    For Each CTL In Me.Frame1.Controls
        TEST = CTL.Name
        If  Left(TEST, 1) = "B" Then
            ' Do something with the control
            Me.CTL.Name.ForeColor = vbRed<<< error here
            'Me.TEST.
        End If
    Next CTL
You do not have the required permissions to view the files attached to this post.
Last edited by sal21 on 25 Jun 2014, 19:25, edited 1 time in total.

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

Re: loop / iterate label in frame

Post by HansV »

Change the line

Code: Select all

        If TypeName(ctl) = "Label" Then
to

Code: Select all

        If TypeName(ctl) = "Label" And ctl.Name Like "B*" Then
Best wishes,
Hans

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

Re: loop / iterate label in frame

Post by sal21 »

HansV wrote:Change the line

Code: Select all

        If TypeName(ctl) = "Label" Then
to

Code: Select all

        If TypeName(ctl) = "Label" And ctl.Name Like "B*" Then

sorry me Hans but changed post with my new request if the label is matched ...

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

Re: loop / iterate label in frame

Post by HansV »

It's difficult to help you if you keep on changing the question after each reply... :sad:
Best wishes,
Hans

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

Re: loop / iterate label in frame

Post by HansV »

CTL.Name does not have a ForeColor. CTL has a ForeColor.
Best wishes,
Hans

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

Re: loop / iterate label in frame

Post by sal21 »

HansV wrote:CTL.Name does not have a ForeColor. CTL has a ForeColor.
Sorry if i post on old 3d...

i need if to check if at least texbox have a no null value ...

Code: Select all

Dim cCont As Control

    For Each cCont In Me.Controls
        If TypeName(cCont) = "TextBox" And cCont.Name Like "P*" Then
            'here the counter +1 to check the condition but how to intercept a blank value in tetxbox? 
        End If
    Next cCont

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

Re: loop / iterate label in frame

Post by HansV »

Like this:

Code: Select all

    Dim cCont As Control
    Dim lConta As Long

    For Each cCont In Me.Controls
        If TypeName(cCont) = "TextBox" And cCont.Name Like "P*" Then
            If cCont.Value <> "" Then
                lConta = lConta + 1
            End If
        End If
    Next cCont

    If lConta = 0 Then
        ' All text boxes are blank
    Else
        ' At least one text box is not blank
    End If
Best wishes,
Hans