How do I Dynamically identify the frame of an active control?

MPW

Well-known Member
Joined
Oct 7, 2009
Messages
571
Office Version
  1. 365
Platform
  1. Windows
I am using the age old loop through the controls ie.
Code:
                For Each cCont1 In Workshop.Frame2.Controls
                    If TypeName(cCont1) = "TextBox" Then
                        etc., etc.

My question is "How do I identify the frame that the control resides in?" I have 10 frames in frame2 and they all have identical parameters. Ultimately what I am attempting to do is to loop through each of the 10 frames to populate labels based on the textboxes in those same frames.

I know how to do it with folders and sub folders, but I am not sure how to do it with objects in a userform.

Any help is greatly appreciated.
 

Excel Facts

Does the VLOOKUP table have to be sorted?
No! when you are using an exact match, the VLOOKUP table can be in any order. Best-selling items at the top is actually the best.
You should be able to use something like
Code:
 cCont.Parent.Name

That should return the Frame name or if it is a control on the Userform, it will return the name of the userform
 
Last edited:
Upvote 0
Tried .parent - gave me "object required".
Just tried .parent.name with the same results.

I don't think this should be this hard.

Thanks for trying. I am sure it is something like that. That is why they invented hammers, right?
 
Upvote 0
"How do I identify the frame that the control resides in?"

If you have the name of the control, eg. Textbox1, then

Code:
wFrame = TextBox1.Parent.Name
 
Upvote 0
Solution
I already rewrote the version that gave the error using .parent.name.
For now I compromised by using this.
Code:
    Dim cCont As Control
    Dim cCont1 As Control
    
    For Each cCont In Workshop.Frame2.Controls
        If TypeName(cCont) = "Label" Then
            If cCont.Top = 144 Then
                cCont.Caption = ""
            End If
        End If
    Next cCont
    
    For Each cCont1 In Workshop.Frame2.Controls
        If TypeName(cCont1) = "TextBox" Then
            If cCont1.Text <> "" Then
                [B]For Each cCont In Workshop.Frame2.Controls[/B]
                    If TypeName(cCont) = "Label" Then
                        If cCont.Top = 144 Then
                            If cCont.Caption = "" Then
                                If cCont.Left = cCont1.Left Then
                                    cCont.Caption = Len(cCont1.Text)
                                    Exit For
                                End If
                            End If
                        End If
                    End If
                Next cCont
            End If
        End If
    Next cCont1

Optimally I would like to use the label that is positioned directly below each textbox. If I could get the parent statement to work for the imbedded for loop then I would be a happy man. You can see that this version is very weak and I could possibly get the wrong information in a label.
 
Upvote 0
You can relate the names of the labels and the text box.
For example Tb1 with Lb1, Tb2 with Lb2, Tb3 with Lb3. In this way it is not necessary to revise the Frame or the position of the Label, simply you know that the Tb1 corresponds with its Lb1.
The code would look like this:

Code:
    For i = 1 To 3
        If Me.Controls("Tb" & i).Value <> "" Then
            Me.Controls("Lb" & i).Caption = Me.Controls("Tb" & i).Value
        End If
    Next


You will only have to change once the name of all your label and texbox that are related.
 
Upvote 0
I think I understand your logic, however I would have to load 60 variables. I could use this method, but would like to learn how to do the other. I would rather work directly with the objects.

With the method that I posted I could get a good result if I could dynamically set and search in the same frame that the textbox is located. This would be a good thing to use in other situations.
 
Upvote 0
Thank you DanteAmor. Your first answer is what I needed.
If you have the name of the control, eg. Textbox1, then

Code:

wFrame = TextBox1.Parent.Name

I just replaced:
Code:
For Each cCont In Workshop.Frame2.Controls
with
Code:
For Each cCont In cCont1.Parent.Controls
This gave me what I needed, thanks again for you patience.
 
Upvote 0

Forum statistics

Threads
1,223,885
Messages
6,175,183
Members
452,615
Latest member
bogeys2birdies

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top