PDA

View Full Version : Get container and/or parent of dynamically created control (checkbox)



melvin
12-14-2014, 06:38 AM
I have dynamically created checkboxes by the use of a class module.
I also have a dynamically created "Select All" checkbox. The purpose of this "Select all" checkbox is to set all other checkboxes in the same frame to the same value as the "Select all" checkbox.

Can I use the parent or container property for dynamically created checkboxes (or controls in general)? Would that require that I specify the parent or container property in the class module (of course, I guess)?

How would I have to go about this?

Thanks!

snb
12-14-2014, 08:43 AM
Why don't you design all controls in design mode and only make them visible/invisible at run time ?

mikerickson
12-14-2014, 09:02 AM
I agree with snb that dynamically created controls are problematic, but if you create a control you can use the .Parent property to get it's container, which might be a user form, a Frame or a Page from a multipage control.

Another way would be to use a (multi-select) ListBox with .ListStyle set to fmListStyleOption. Each dynamic checkbox could be a list item in such a listbox.

This routine will return the user form object in which a control resides (not just the Frame/Page container).

Function ParentUF(ctrl As MSForms.Control) As Object
Set ParentUF = ctrl
On Error Resume Next
Do
Set ParentUF = ParentUF.Parent
Loop Until Err
On Error GoTo 0
End Function