ComboBox For Loop

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • jbott59
    New Member
    • Jun 2007
    • 7

    #1

    ComboBox For Loop

    I have 10 comboboxes. i would like to run a loop that would reference each combo box and resets certain values. The comboboxes are named ComboBox1, Combobox2, ..... ComboBox10. Here is a sample of what i want. The error returned is "Sub or Function not defined".
    [CODE=vb]Public Sub ResetValues()
    For i = 1 to 10
    ComboBox(i).lis tfillrange=""
    'the code errors here where I want to call the combobox
    ComboBox(i).vis ible = True
    ....etc
    Next
    End Sub[/CODE]
  • kadghar
    Recognized Expert Top Contributor
    • Apr 2007
    • 1302

    #2
    Originally posted by jbott59
    I have 10 comboboxes. i would like to run a loop that would reference each combo box and resets certain values. The comboboxes are named ComboBox1, Combobox2, ..... ComboBox10. Here is a sample of what i want. The error returned is "Sub or Function not defined".
    [ Public Sub ResetValues()
    For i = 1 to 10
    ComboBox(i).lis tfillrange=""
    'the code errors here where I want to call the combobox
    ComboBox(i).vis ible = True
    ....etc
    Next
    End Sub]
    maybe your index starts from zero... you should try FOR i = 0 to 9

    Comment

    • kadghar
      Recognized Expert Top Contributor
      • Apr 2007
      • 1302

      #3
      oh, and i didnt noticed. all your combo boxes must have the same name. Lets say: ComboBoxes and you have to make an array of comboboxes with the same name but different indexes.

      This is harder to do with VBA (not even sure if you can do it) but with any other version of VB you have "Index" in the properties menu.

      Good Luck

      Comment

      • jbott59
        New Member
        • Jun 2007
        • 7

        #4
        thanks i'll check it out

        Comment

        • Killer42
          Recognized Expert Expert
          • Oct 2006
          • 8429

          #5
          You have two options. If this is VB6 (or another compatible version) you should make the controls into a "control array". That is, they should all have the same name, and only differ in their Index value. That way, you can refer to them in a loop by just changing the index number, the same as an ordinary array.

          If you're using VB.Net (effectively any VB version later than VB6, which is about 10 years old) control arrays are not supported any more (thanks a lot, M$!). You can get around it by using your index number to build a string, such as strControlName = "ComboBox" & Format(i), and then using this construct to access it... Me.Controls(str ControlName)

          Comment

          Working...