UserForm Control Array? 
Author Message
 UserForm Control Array?

How do I create a control array of textboxes on a
userform?

Robert



Fri, 04 Mar 2005 22:48:36 GMT  
 UserForm Control Array?
VBA does not support Control Arrays.

--
http://www.standards.com/; Programming and support for  Word macros,
including converting from WordBasic to VBA; Technical reviewing; Standards;
Product functional/design/specifications
------------------------------------------------

Quote:
> How do I create a control array of textboxes on a
> userform?

> Robert



Fri, 04 Mar 2005 23:02:43 GMT  
 UserForm Control Array?
Can't be done. VBA - unlike VB - doesn't support control arrays.


Quote:
> How do I create a control array of textboxes on a
> userform?

> Robert



Fri, 04 Mar 2005 23:23:30 GMT  
 UserForm Control Array?
Hi, Robert,

As Howard and Marilyn told you, VBA doesn't support control arrays, at least not
like the ones in VB. However, you can fake it, depending on what you need to do.

If you just want to cycle through all the textboxes on the userform, to
initialize them or to validate their contents, you can do it this way:

Private Sub CommandButton1_Click()
   Dim oCtl As Control
   For Each oCtl In Controls
      If TypeOf oCtl Is MSForms.TextBox Then
         If oCtl.Value <> "" Then
            oCtl.BackColor = &HFF0000
         End If
      End If
   Next oCtl
End Sub

If you need to address them individually, the trick is to give them all the same
root name plus a numeric suffix, perhaps accepting their default names TextBox1,
TextBox2, ... Then the loop can assign or check the value of the suffix:

Private Sub CommandButton1_Click()
   Dim oCtl As Control
   Dim idx As Integer

   For idx = 1 To 7 Step 2
      Controls("TextBox" & idx).Value = "foo"
   Next idx

   For Each oCtl In Controls
      If TypeOf oCtl Is MSForms.TextBox Then
         If Val(Right(oCtl.Name, 1)) > 4 Then
            oCtl.BackColor = &HFF0000
         End If
      End If
   Next oCtl
End Sub

--
Regards,
Jay Freedman
Microsoft Word MVP        Word MVP FAQ site: http://www.mvps.org/word

Quote:

> How do I create a control array of textboxes on a
> userform?

> Robert



Sat, 05 Mar 2005 03:07:59 GMT  
 UserForm Control Array?
Hi Robert,

You can use the tequnique Jay describes or this one I frequently use.

Below is an example I posted to a similar question a while back.  

You can also put your controls into an array and then go through the
items in an the array to check the status of each one.

The advantage of this is that you can then use any name for the
control.  You are not restricted to names like checkbox1 checkbox2
etc.

To see what I mean paste the following code into the declarations
section of a form into which you have places a commandbutton  called
"CommandButton1" and four checkboxes called  "cbApples", "cbOranges",
"cbPears", "cbLemons"

Run the form and select one or more of the items then click the button

'*****Start of code *****
Dim cbAy() As Object
Const NumItems = 4

Private Sub CommandButton1_Click()
    For I = 1 To NumItems
        With cbAy(I)
            If .Value = True Then
                msg = msg & .Caption & vbCr
            End If
        End With
    Next

    MsgBox "The following were selected " & vbCr & msg
End Sub

Private Sub UserForm_Initialize()
    ReDim cbAy(NumItems)
    Set cbAy(1) = cbApples
    Set cbAy(2) = cbOranges
    Set cbAy(3) = cbPears
    Set cbAy(4) = cbLemons
End Sub
'***** End of code *****

Hope this Helps
Cheers
TonyS.

Quote:

> How do I create a control array of textboxes on a
> userform?

> Robert



Sun, 06 Mar 2005 15:42:45 GMT  
 
 [ 5 post ] 

 Relevant Pages 

1. UserForms and Control Arrays

2. Printing from userforms/arrays in XL97 VBA

3. Contro Array on UserForm

4. Unloading an array to TextBoxes on a UserForm

5. DLLs & Control Arrays + Arrays of Controls

6. UserForm updating a 2nd UserForm?

7. Two Questions - Making a userform active / Accessing a userform via a variable

8. How to center a Form to the UserForm it belongs to, if the UserForm has moved

9. Each item in an array in an array of controls

10. Indexed Arrays/Control Arrays Help

11. Arrays of Control Arrays

12. Array limits of a control array?

 

 
Powered by phpBB® Forum Software