how to create multiple controls at userform load such as textbox.

H

Herve cadieu

In vb programs there are often controls arrays
such as command1(0),command1(1), command1(2)

and some code which reacts to specific index:

sub command1_click(index as integer)
if index =1 then
do something
else
do something else
end if
end sub

Ok I am looking for the way to create such controls array in VBA, to use
these kind of VB code in VBA projects .

I have tried to name a control command1(0) but it is not a valid name
how can I proceed to create these command(n) controls ?

Thank you for your knowledge
best regards
 
H

Helmut Weber

Hi Herve,
this is an example for a workaraound
for creating arrays of controls, here for checkboxes:

Private Sub UserForm_Initialize()
' Count Optionbuttons
Dim oCnt As Control
Dim iCnt As Integer
For Each oCnt In Me.Controls
If TypeOf oCnt Is MSForms.OptionButton Then
iOpt = iOpt + 1
End If
Next
' create array of optionbuttons
ReDim ArrOpt(iOpt) As OptionButton
' assign each optionbutton
For Each oCnt In Me.Controls
If TypeOf oCnt Is MSForms.OptionButton Then
iCnt = iCnt + 1
Set ArrOpt(iCnt) = oCnt
' setting caption for testing
ArrOpt(iCnt).Caption = Format(iCnt, "Opt- 00")
End If
Next
End Sub

Greetings from Bavaria, Germany

Helmut Weber, MVP
"red.sys" & chr(64) & "t-online.de"
Word XP, Win 98
http://word.mvps.org/

Note, that the "for each loop" over all controls,
seemably, processes them in the order they were created.

Greetings from Bavaria, Germany

Helmut Weber, MVP
"red.sys" & chr(64) & "t-online.de"
Word XP, Win 98
http://word.mvps.org/
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top