Roldy
02-03-2020, 01:13 PM
I have a userform with a bunch of text boxes which I added to a collection using the text boxes name as the index string. This code is in the initialize event of the userform. The user enters data and clicks on a button. This button runs a sub to gather the data in each text box. Since I already have my collection of text boxes, I don't need to rebuild it...I just need to get the control so I can get the value that is in it. What is the correct way of looping through the collection to get the control? In the form initialization, I am building the collection using a custom class. I'm doing it this way because later down the road I'm going to use this class to control some text box events.
Private Sub UserForm_Initialize()
Set colTbxs = New Collection
For Each nControl In Me.Controls
Select Case TypeName(nControl)
Case "TextBox"
Set clsObject = New clsObjectHandler
Set clsObject.Control = nControl
colTbxs.Add clsObject, nControl.Name
Case Else
End Select
Next nControl
End Sub
In the button click event I do For Each loop to try and access each item in the collection. This is where I am having problems. As a test, I'm trying to display the name of the control but can't figure out how to access any of the properties. The image shows that it has a class instance of my custom class. I suspect I need to use the Set command.
Dim nCol as Variant
For Each nCol In colTbxs
????
Next nCol
25915
Private Sub UserForm_Initialize()
Set colTbxs = New Collection
For Each nControl In Me.Controls
Select Case TypeName(nControl)
Case "TextBox"
Set clsObject = New clsObjectHandler
Set clsObject.Control = nControl
colTbxs.Add clsObject, nControl.Name
Case Else
End Select
Next nControl
End Sub
In the button click event I do For Each loop to try and access each item in the collection. This is where I am having problems. As a test, I'm trying to display the name of the control but can't figure out how to access any of the properties. The image shows that it has a class instance of my custom class. I suspect I need to use the Set command.
Dim nCol as Variant
For Each nCol In colTbxs
????
Next nCol
25915