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.
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):
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: