2
votes

I have a number of userforms with static controls on them. I have created a Class Module to apply event handlers to a set of texboxes I am dynamically adding to the userform. When I specifically address a userform from the Class Module I can update the value in the static controls...

IAFStep2c.Controls("chkOptIn").Value = True

But when I try to update the controls with their userform's name in a variable it doesn't work...

Dim oUserFOrm As Object
Dim formName As String
formName = "IAFStep2c"
Set oUserFOrm = UserForms.Add(formName)
oUserFOrm.Controls("chkOptIn").Value = True

I can read the value and other properties of the controls, just not update them. Can someone offer a solution to this?

EDIT

I created another spreadsheet with the class below, same result. TextBox1 and 3 get updated, textBox2 does not.

Sub doStuff()
'MsgBox ("ping")
Dim oUserFOrm As Object
Dim formName As String
formName = "frmTest"
Set oUserFOrm = UserForms.Add(formName)
frmTest.Controls("TextBox1").Value = oUserFOrm.Controls("TextBox2").Name
oUserFOrm.Controls("TextBox2").Value = "PING"
frmTest.Controls("TextBox3").Value = oUserFOrm.Controls("TextBox2").TextAlign

End Sub

1
Why someone downvoted this question? It's properly formulated! - Maciej Los
I can't reproduce such of behaviour. What version of MS Excel? What's error message? Have you tried to export userform, delete it and import it again? Can you show your class definition? - Maciej Los
Excel 2013. There is no error message, it just fails to update the field and continues on as if nothing happened. Not tried export/import of the form, I'm new to this so any suggestions will be appreciated. - James Gray
UserForms.Add(formName) will Add the UserForm as a new UserForm to memory. It takes the UserForm from designer with properties and values provided in designer. It will not get an already shown UserForm. - Axel Richter
Thanks Axel, that makes sense. Still haven't found how to get an already shown userform :( - James Gray

1 Answers

1
votes

This is how I do it:

UserForm1.Controls("Textbox1").Text = "123"

At least it works by me, when I am trying it. Office 2010.