• Control Arrays in VBA? (Word 2000/2002)

    Home » Forums » AskWoody support » Productivity software by function » Visual Basic for Applications » Control Arrays in VBA? (Word 2000/2002)

    • This topic has 3 replies, 3 voices, and was last updated 22 years ago.
    Author
    Topic
    #388778

    It appears that unlike VB, VBA for Word does not support control arrays.
    I’ve got four checkboxes in each of 4 frames on a form, with the same tests needed for each set of 4 checkboxes. I’ve tried creating the checkbox names using strings concatenated with numeric indexes, but I haven’t yet found a what to turn the resulting strings into variable or checkbox names. Is there any solution for this, some string to variable name conversion?

    Thanks,
    Steve

    Viewing 1 reply thread
    Author
    Replies
    • #683966

      UserForms in Microsoft Office are different from VB6 forms, as you found out. You can use the Controls property of a UserForm to refer to a control by its name as a string. Let’s say that you have check boxes named CheckBox1 to CheckBox4 on a form named UserForm1.

      Dim i As Integer
      For i = 1 To 4
      MsgBox UserForm1.Controls(“CheckBox” & i).Value
      Next i

      Note : if this code is run on the form itself, you can use Me.Controls instead of UserForm1.Controls.

    • #684123

      If there’s nothing else in the frames, you could use the Frame’s Controls collection. I played with 2 frames with 2 checkboxes each and this code, and it seems to work:

      Private Sub UserForm_Click()
      Dim ctrl As MSForms.Control
      For Each ctrl In Me.Frame1.Controls
      Debug.Print ctrl.Name, ctrl.Value
      Next
      For Each ctrl In Me.Frame2.Controls
      Debug.Print ctrl.Name, ctrl.Value
      Next
      End Sub

      I’m not sure you can assume that the order will be anything in particular, ‘though I think “order added” is the most likely.

    Viewing 1 reply thread
    Reply To: Control Arrays in VBA? (Word 2000/2002)

    You can use BBCodes to format your content.
    Your account can't use all available BBCodes, they will be stripped before saving.

    Your information: