Can the textboxes be treated as an array of elements?

C

cyberdude

Hi,

I have added a number of textboxes in a userform. They range from
textbox1 to textbox15. The coding will become cumbersome if I code
the 15 textboxes separately. For example, to initialize the 15
textboxes, I need to write

textbox1=""
textbox2=""
....
textbox15=""

May I ask if there is a way to treat the textboxes as an array of
elements like

Dim textbox(1 to 15)

To initialize, I only need to write

for i=1 to 15
textbox(i)=""
next i

Thanks

Mike
 
H

Helmut Weber

Hi Cyberdude,

Unlike VB, VBA doesn't provide for arrays of controls
automatically. However, one can force it, by defining arrays of
controls, like (global):
Dim arTxt() As TextBox
Dim arLbl() As Label
The index will refer to the sequence, in which the were created.
Assigning each control to a indexed control object:
Private Sub UserForm_Initialize()
Dim i As Integer
Dim oCnt As Control
For Each oCnt In Me.Controls ' count textboxes = count lables
If TypeOf oCnt Is MSForms.TextBox Then
iCnt = iCnt + 1
End If
Next
ReDim arTxt(iCnt) As TextBox
ReDim arLbl(iCnt) As Label
iCnt = 0
For Each oCnt In Me.Controls
If TypeOf oCnt Is MSForms.TextBox Then
iCnt = iCnt + 1
Set arTxt(iCnt) = oCnt
End If
Next
iCnt = 0
' assign individual controls to the array of controls
For Each oCnt In Me.Controls
If TypeOf oCnt Is MSForms.Label Then
iCnt = iCnt + 1
Set arLbl(iCnt) = oCnt
End If
Next
End Sub
---
From now on it's easy:
---
Private Sub CommandButton1_Click()
Dim i As Integer
For i = 1 To iCnt
If arTxt(i) <> "" Then
arLbl(i) = "done"
End If
Next
End Sub


--

Greetings from Bavaria, Germany

Helmut Weber, MVP WordVBA

Vista Small Business, Office XP
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top