Controlarray in VBA

V

Vera

Hi there!

I wonder if it is possible to create a controlarray in VBA.
I have a form with a lot of similar controls that require similar handling.
A controlarray with indexed event-handlers would be ideal in a situation
like this.

Is this possible? If so, how? If not, then what can I do to avoid getting
about a hundred similar event-handlers?

Any help will be greatly appreciated.
Thanx!

Vera
 
C

Chirag

You might want to try the following:
1. For all the similar controls, set the index of the control in its Tag
property.
2. Write a function that would have the event handler code and accepts the
index as one of its input parameter.
3. Create event handler for the controls that call function of (2) with the
Tag of the control as input to the function.

While this technique does require a single line event handler - one for each
control, but it comes close to VB control arrays.

- Chirag

PowerShow - View multiple PowerPoint slide shows simultaneously
http://officeone.mvps.org/powershow/powershow.html
 
H

Helmut Weber

Hi Vera,
just another workaraound, here for optionbuttons,
and a creation of an array at runtime.

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

Remember, that "for each" will process the optionbuttons
according to the time (or sequence) of creation.

Greetings from Bavaria, Germany

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