I'm trying to create an array of controls on a userform which are created at runtime, which I can then reference using an index number to define events.
My problem is that I'm declaring a dynamic array of controls using:
Dim textbox() As Object
and then in the Initialization event for the userform, I have the line:
Set textbox(c) = userform.Controls.Add("Forms.Textbox.1"
to add new textbox controls.
But now, once I have the array of textbox controls, I can't define an event for each of the controls in the array. I.e. the syntax:
Private Sub textbox(c)_Change()
gettext = textbox(c).Text
End Sub
doesn't work because VBA doesn't recognize textbox(c)_Change() syntax.
It has been suggested that I use:
Private Sub textbox_Change(Index as Integer)
gettext = textbox(c).Text
End Sub
but this doesn't work either because VBA doesn't recognize textbox_Change(Index as Integer) as referring to the entire array of textbox controls and the event never fires, at least not the way I've declared the array.
I have to add controls to the array at runtime and not at designtime because the number of textbox controls in the array depends on an integer variable.
Any advice would be much appreciated.
My problem is that I'm declaring a dynamic array of controls using:
Dim textbox() As Object
and then in the Initialization event for the userform, I have the line:
Set textbox(c) = userform.Controls.Add("Forms.Textbox.1"
to add new textbox controls.
But now, once I have the array of textbox controls, I can't define an event for each of the controls in the array. I.e. the syntax:
Private Sub textbox(c)_Change()
gettext = textbox(c).Text
End Sub
doesn't work because VBA doesn't recognize textbox(c)_Change() syntax.
It has been suggested that I use:
Private Sub textbox_Change(Index as Integer)
gettext = textbox(c).Text
End Sub
but this doesn't work either because VBA doesn't recognize textbox_Change(Index as Integer) as referring to the entire array of textbox controls and the event never fires, at least not the way I've declared the array.
I have to add controls to the array at runtime and not at designtime because the number of textbox controls in the array depends on an integer variable.
Any advice would be much appreciated.