Results 1 to 17 of 17

Thread: Control-array in VBA

  1. #1

    Thread Starter
    I don't do your homework! opus's Avatar
    Join Date
    Jun 2000
    Location
    Good Old Europe
    Posts
    3,863

    Control-array in VBA

    Isn't it possible to use a control array in VBA???
    You're welcome to rate this post!
    If your problem is solved, please use the Mark thread as resolved button


    Wait, I'm too old to hurry!

  2. #2
    Evil Genius alex_read's Avatar
    Join Date
    May 2000
    Location
    Espoo, Finland
    Posts
    5,538
    Nope, not even with XP/2002. Not sure why but it's not possible!

    Please rate this post if it was useful for you!
    Please try to search before creating a new post,
    Please format code using [ code ][ /code ], and
    Post sample code, error details & problem details

  3. #3
    New Member
    Join Date
    Mar 2003
    Posts
    6
    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
    Attached Files Attached Files

  4. #4
    New Member
    Join Date
    Dec 2003
    Posts
    3

    Re: Control-array in VBA

    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:
    1. Dim arCommandButton(1 To 3) As New Class1
    2.  
    3. Private Sub init()
    4.     Set arCommandButton(1).ButtonEvents = cmd1
    5.     Set arCommandButton(2).ButtonEvents = cmd1
    6.     Set arCommandButton(3).ButtonEvents = cmd1
    7. End Sub
    8.  
    9. 'In a Class Module
    10. Public WithEvents ButtonEvents As MSForms.CommandButton
    11.  
    12. Private Sub ButtonEvents_Click()
    13.    
    14.     MsgBox "Button clicked."
    15.    
    16. End Sub

    Thank you for any help you can give:
    Last edited by jsanford; Nov 1st, 2005 at 11:16 AM.

  5. #5
    Frenzied Member
    Join Date
    Feb 2003
    Location
    Argentina
    Posts
    1,950

    Re: Control-array in VBA

    Throw the index in the tag property of the the particulat textbox and then check that.
    Tengo mas preguntas que contestas

  6. #6
    New Member
    Join Date
    Dec 2003
    Posts
    3

    Re: Control-array in VBA

    A Command Button does not have a tag property

  7. #7
    Frenzied Member
    Join Date
    Feb 2003
    Location
    Argentina
    Posts
    1,950

    Re: Control-array in VBA

    I see one on the "Other" tab of a command button's properties, and in the properties window of the VBE in Access 2000.
    Tengo mas preguntas que contestas

  8. #8
    New Member
    Join Date
    Mar 2003
    Posts
    6

    Re: Control-array in VBA

    Just create your own 'Index' property.

    VB Code:
    1. Private arCommandButton(1 To 3) As New Class1
    2.  
    3. Private Sub init()
    4.     Set arCommandButton(1).ButtonEvents = cmd1
    5.     arCommandButton(1).Index = 1
    6.     Set arCommandButton(2).ButtonEvents = cmd2
    7.     arCommandButton(2).Index = 2
    8.     Set arCommandButton(3).ButtonEvents = cmd3
    9.     arCommandButton(3).Index = 3
    10. End Sub
    11.  
    12. 'In a Class Module
    13. Public WithEvents ButtonEvents As MSForms.CommandButton
    14. Public Index As Integer
    15.  
    16. Private Sub ButtonEvents_Click()
    17.     MsgBox "Button clicked.  The Index is: " & Index
    18. End Sub
    Last edited by tstom1970; Nov 1st, 2005 at 09:18 PM.

  9. #9
    New Member
    Join Date
    Dec 2003
    Posts
    3

    Re: Control-array in VBA

    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??

  10. #10
    New Member
    Join Date
    Nov 2005
    Posts
    1

    Re: Control-array in VBA

    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

  11. #11
    New Member
    Join Date
    Nov 2006
    Posts
    5

    Re: Control-array in VBA

    I can get the index but how do I return the name of the image I clicked on?

  12. #12
    New Member
    Join Date
    Jan 2012
    Posts
    2

    Re: Control-array in VBA

    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

  13. #13
    Frenzied Member
    Join Date
    Nov 2010
    Posts
    1,470

    Re: Control-array in VBA

    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

  14. #14
    New Member
    Join Date
    Jan 2012
    Posts
    2

    Re: Control-array in VBA

    thanks Mr, But my program has 59 buttons..
    must I do that for every single button?

  15. #15
    Frenzied Member
    Join Date
    Nov 2010
    Posts
    1,470

    Re: Control-array in VBA

    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?

  16. #16
    Discovering Life Siddharth Rout's Avatar
    Join Date
    Feb 2005
    Location
    Mumbai, India
    Posts
    12,001

    Re: Control-array in VBA

    Quote Originally Posted by opus View Post
    Isn't it possible to use a control array in VBA???
    Yes it is possible.

    See this link.

    Topic: VBA – Control Arrays
    Link: http://www.siddharthrout.com/index.p...ontrol-arrays/
    Last edited by Siddharth Rout; Jul 17th, 2020 at 09:07 PM. Reason: Updated Link
    A good exercise for the Heart is to bend down and help another up...
    Please Mark your Thread "Resolved", if the query is solved


    MyGear:
    ★ CPU ★ Ryzen 5 5800X
    ★ GPU ★ NVIDIA GeForce RTX 3080 TI Founder Edition
    ★ RAM ★ G. Skill Trident Z RGB 32GB 3600MHz
    ★ MB ★ ASUS TUF GAMING X570 (WI-FI) ATX Gaming
    ★ Storage ★ SSD SB-ROCKET-1TB + SEAGATE 2TB Barracuda IHD
    ★ Cooling ★ NOCTUA NH-D15 CHROMAX BLACK 140mm + 10 of Noctua NF-F12 PWM
    ★ PSU ★ ANTEC HCG-1000-EXTREME 1000 Watt 80 Plus Gold Fully Modular PSU
    ★ Case ★ LIAN LI PC-O11 DYNAMIC XL ROG (BLACK) (G99.O11DXL-X)
    ★ Monitor ★ LG Ultragear 27" 240Hz Gaming Monitor
    ★ Keyboard ★ TVS Electronics Gold Keyboard
    ★ Mouse ★ Logitech G502 Hero

  17. #17
    Discovering Life Siddharth Rout's Avatar
    Join Date
    Feb 2005
    Location
    Mumbai, India
    Posts
    12,001

    Re: Control-array in VBA

    Damn! I just noticed the date of the 1st post!
    A good exercise for the Heart is to bend down and help another up...
    Please Mark your Thread "Resolved", if the query is solved


    MyGear:
    ★ CPU ★ Ryzen 5 5800X
    ★ GPU ★ NVIDIA GeForce RTX 3080 TI Founder Edition
    ★ RAM ★ G. Skill Trident Z RGB 32GB 3600MHz
    ★ MB ★ ASUS TUF GAMING X570 (WI-FI) ATX Gaming
    ★ Storage ★ SSD SB-ROCKET-1TB + SEAGATE 2TB Barracuda IHD
    ★ Cooling ★ NOCTUA NH-D15 CHROMAX BLACK 140mm + 10 of Noctua NF-F12 PWM
    ★ PSU ★ ANTEC HCG-1000-EXTREME 1000 Watt 80 Plus Gold Fully Modular PSU
    ★ Case ★ LIAN LI PC-O11 DYNAMIC XL ROG (BLACK) (G99.O11DXL-X)
    ★ Monitor ★ LG Ultragear 27" 240Hz Gaming Monitor
    ★ Keyboard ★ TVS Electronics Gold Keyboard
    ★ Mouse ★ Logitech G502 Hero

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width