How to get Macro to loop through multiple frames

ilya2004

Board Regular
Joined
Mar 17, 2011
Messages
135
Hello,

I have a macro that creates a background image behind every textbox in a userform. The textbox is transparent so the image becomes like the background for the textbox.

the macro esentially goes like this:

Code:
    Dim box As Control
    For Each box In Me.Controls
        'Code to create back image
    Next

Now I moved some of the boxes onto different frames.

I know I could do:
Code:
For Each box in Me.Frame1.Controls
but I wanted to know if there was a way I could get it to loop through all of the frames and do it automatically.

Thanks!
-Ilya
 

Excel Facts

How to show all formulas in Excel?
Press Ctrl+` to show all formulas. Press it again to toggle back to numbers. The grave accent is often under the tilde on US keyboards.
Looping through each control in the Userform's controls collection should catch all the controls in each frame.
Code:
Dim oneControl as Object

For each oneControl in Userform1.Controls
    If TypeName(oneControl) = "TextBox" Then
        MsgBox oneControl.Name & " is in " & oneControl.Parent.Name
    End If
Next oneControl
 
Upvote 0
Hi Mike,

Thanks for your help, I was able to realize what my error was. It wasn't that Excel did not identify the boxes, but the background image that I was trying to create was being planted in the module itself, and not in the frame behind the textbox where it should have. How do I get it to dynaimcally create the image in the frame. Here is my whole code:

Code:
Sub BoxBG()
Dim cbBG As Control
Dim obBox As Control
 
For Each obBox In frmMain.Controls
If obBox.Name Like "i*" Then
Set cbBG = frmMain.Controls.Add("Forms.image.1")
With cbBG
.Visible = True
.Picture = frmIndv2.obBGsource.Picture
.PictureAlignment = 2
.PictureSizeMode = 1
.Width = obBox.Width
.Height = obBox.Height
.Left = obBox.Left
.Top = obBox.Top
.Name = obBox.Name & "bg"
.ZOrder 1
.BorderStyle = 0
End With
End If
Next
End Sub
 
Last edited:
Upvote 0
I should also clarify that the controls are in a multi-page now, not in a frame as they were before
 
Upvote 0
Instead of adding to frmMain's Control collection, add to the obBox's container's Controls Collection.
Code:
Set cbBG = obBox.Parent.Controls.Add("Forms.image.1")
 
Upvote 0
that's what brings me back to the problem I had to begin with, I have 7 or 8 different parents for all the controls, so i want to automatically generate them in each parent.
 
Upvote 0
Each control has only one parent.
If you tried the test routine in post#2, you'll see that the .Parent of a control is the bottom level container of the control.
i.e. if myForm contains MultiPage1, Page(0) of which has Frame8 which contains TextBox8, then
TextBox8.Parent is Frame8
TextBox8.Parent.Parent is Page0
TextBox8.Parent.Parent.Parent is Multipage1
TextBox8.Parent.Parent.Parent.Parent is myForm
TextBox8.Parent.Parent.Parent.Parent.Parent errors.

The point of which is that as you loop through Each oneControl in Userform1.Controls, the .Parent property of oneControl will get all the containers.
 
Upvote 0
I realize that I wasn't clear before, I figured out how to get it to do what I wanted. I simply preceded the code I posted above with the following:

Dim pg As Control
For Each pg In frmMain.mpMain.Pages
 
Upvote 0

Forum statistics

Threads
1,224,507
Messages
6,179,183
Members
452,893
Latest member
denay

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