Referencing controls on a userform that was created dynamically

AndyGalloway

Board Regular
Joined
Apr 24, 2019
Messages
51
I have created a Userform programmatically and created a series of controls on the form. Now I want to read from/write to one of the text boxes. I have tried everything that I can think of to reference to the control, but I'm having no luck. The control was originally created as part of the process of creating the Userform. The control was created using the following code.

Rich (BB code):
Dim myUserForm As VBComponent
Dim objNewJobTitle As MSForms.TextBox
Dim f As Integer: f = 1
Dim varNewJobTitles() as String

    varNewJobTitles(1) = "Managing Director"
    varNewJobTitles(2) = "Operations Director"
    varNewJobTitles(3) = "Office Manager"

Set myUserForm = ActiveWorkbook.VBProject.VBComponents.Add(vbext_ct_MSForm)

With myUserForm
    For f = 1 to 3

    Set objNewJobTitle = myUserForm.Designer.Controls.Add("Forms.TextBox.1", "txtNewJobTitle" & f, True)
        With objNewJobTitle
            .Left = 38  '148
            .Width = 100
            .Height = 18
            .Top = 40 + (18 * f)
            .Value = varNewJobTitles(f)
        End With
    Next

end with


I have tried the obvious of referencing the control using myUserForm.txtNewJobTitle3.Text, but get an error. I have also tried several versions and variations of the following code.

Rich (BB code):
    Set objNewJobTitle = myUserForm.Designer.Controls.Add("Forms.TextBox.1", "txtNewJobTitle" & f, True)
    With objNewJobTitle
        .Value = "Hello"
    End With

    With ThisWorkbook.VBProject.VBComponents("myUserForm").Designer
        .Controls("txtNewJobTitle3").Value = "Hello"
    End With

    With ThisWorkbook.VBProject.VBComponents("myUserForm").Designer
        .Controls("Forms.TextBox.1", "txtNewJobTitle3", True).Text = "Hello"
    End With
 
Last edited:
The code I gave you loops through the "handler" collections - handlers isn't a specific tehnical term. Specifically, which bit doesn't work for you?
 
Upvote 0

Excel Facts

Quick Sum
Select a range of cells. The total appears in bottom right of Excel screen. Right-click total to add Max, Min, Count, Average.
From post #21
Dear Kyle123, With your help I'm making progress with this. I've copied the above code into the click event of the "Reset" button and changed a few things around so that I am copying "varOldJobTitles()" into each text box in turn. It doesn't work. Immediately after the "For Each" line it skips to the line after "Next". I have posted the entire code below.

Code:
Sub btnReset_Click()
   
    Dim jcc As Variant
    Dim x As Integer: x = 1
    Dim jobControlHandler As JobChangeControl
    Dim r As Integer: r = 1

    r = MsgBox("Are you sure you want to reset this form? You will lose all changes you have made.", _
    vbQuestion + vbOKCancel, "Are you sure?")
   
    If r = vbOK Then
        handlers.Add jobControlHandler
        Set handlers = New Collection
        For Each jcc In handlers
            jcc.JobTitle.Text = varOldJobTitles(x)
            jcc.UnCheck.Value = True
            x = x + 1
        Next jcc
    End If
  
    MsgBox Join(varOldJobTitles, "; ")
   
End Sub
 
Upvote 0
Andy

Have you definitely posted all the code?

I tried copying the code from post #23 and setting up a userform with required command buttons but it keeps on failing here in the class module.
VBA Code:
    varNewJobTitles(x) = LevelAbove.JobTitle.Value
    varNewJobTitles(y) = JobTitle.Value
 
Upvote 0
You shouldn’t be adding more handlers when resetting the form. The reason that it jumps to the next line is that
Set handlers = New Collection erases the items in the collection so there’s nothing in it to loop through
 
Upvote 0

Forum statistics

Threads
1,223,704
Messages
6,173,984
Members
452,540
Latest member
haasro02

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