Isn't it possible to use a control array in VBA???
Printable View
Isn't it possible to use a control array in VBA???
Nope, not even with XP/2002. Not sure why but it's not possible!
You may create an array of control objects and share single event procedures. For example, say you have any number of textboxes on some userform:
Code:'place the following in a userform
Dim AobjTextBoxes() As New Class1
Private Sub UserForm_Initialize()
Dim intCtlCnt As Integer, objControl As Control
For Each objControl In Me.Controls
If TypeOf objControl Is MSForms.TextBox Then
'if you wish to exclude object
'do it here...
intCtlCnt = intCtlCnt + 1
ReDim Preserve AobjTextBoxes(1 To intCtlCnt)
Set AobjTextBoxes(intCtlCnt).TextBoxEvents = objControl
End If
Next objControl
Set objControl = Nothing
End Sub
'place in a Class module
Public WithEvents TextBoxEvents As MSForms.TextBox
Private Sub TextBoxEvents_MouseDown(ByVal Button As Integer, _
ByVal Shift As Integer, ByVal X As Single, ByVal Y As Single)
MsgBox "Mouse down"
End Sub
I realize this is an old thread but hopefully someone will read it.
I am trying to creat a control array of command buttons in VBA similiar to the textbox example.
I am able to get the common event click to work but cannot find a way to to creade an index to determine which button was clicked.
In the sample there are three Command Buttons. This works as is but I need an index for the button clicked. I tried adding Index as Integer and ByVal Index as Integer but got the error:
"Procedure declaration does not match description of event or procedure having the same name."
Here is a sample of what I have:
VB Code:
Dim arCommandButton(1 To 3) As New Class1 Private Sub init() Set arCommandButton(1).ButtonEvents = cmd1 Set arCommandButton(2).ButtonEvents = cmd1 Set arCommandButton(3).ButtonEvents = cmd1 End Sub 'In a Class Module Public WithEvents ButtonEvents As MSForms.CommandButton Private Sub ButtonEvents_Click() MsgBox "Button clicked." End Sub
Thank you for any help you can give:
Throw the index in the tag property of the the particulat textbox and then check that.
A Command Button does not have a tag property
I see one on the "Other" tab of a command button's properties, and in the properties window of the VBE in Access 2000.
Just create your own 'Index' property.
VB Code:
Private arCommandButton(1 To 3) As New Class1 Private Sub init() Set arCommandButton(1).ButtonEvents = cmd1 arCommandButton(1).Index = 1 Set arCommandButton(2).ButtonEvents = cmd2 arCommandButton(2).Index = 2 Set arCommandButton(3).ButtonEvents = cmd3 arCommandButton(3).Index = 3 End Sub 'In a Class Module Public WithEvents ButtonEvents As MSForms.CommandButton Public Index As Integer Private Sub ButtonEvents_Click() MsgBox "Button clicked. The Index is: " & Index End Sub
This is an array that I created at run time and thus does not have propertys such as index. I could actually use the name property of the command button but how do I pass it through the withevents and access it in the buttonevents sub? The real question is can I pass this through to the sub????
It appears from the textbox example from an earlier thread that it is possible but since I am not a skilled programer I am not sure if it is really possible.
Any other comments or ideas??
I used code to set the caption / image and when the button is clicked passed this off into a Function
Might be worth a try
Let me know if you want me to paste code
I can get the index but how do I return the name of the image I clicked on?
Hello,
I've done that, but i want something other.
I want to have a commands array, and labels array with zeros caption.
when I click on a command, the label with the same index will increase.
I pray that the idea is obvious...
yours
simply attach a macro to each button that calls the single button macro with a parameter
i.e.
button 1 calls button1_click
button1_click calls button_click 1
button2_click calls button_click 2
and so on tedious but there is only 1 button event to look after
here to suggest
thanks Mr, But my program has 59 buttons..
must I do that for every single button?
can you not write it using the find replace functions in your word processor?
you can alter the code in another environment
you simply make a macro to add the appropriate text into the module
and then use the edited module
you will then only need to change the button_event(x) routine
and add the button1_event things when you add a new button
does this make any sense to you?
Yes it is possible.
See this link.
Topic: VBA – Control Arrays
Link: http://www.siddharthrout.com/index.p...ontrol-arrays/
Damn! I just noticed the date of the 1st post!