By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
425,461 Members | 2,184 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 425,461 IT Pros & Developers. It's quick & easy.

Control array on form

P: n/a
Is it possible to create a control array on an unbound form? I would
like to be able to loop through a series of unbound text boxes.

Nov 13 '05 #1
Share this Question
Share on Google+
2 Replies

P: n/a
Sort of, but not exactly like in VB6 or VB8 (note: version VB7 doesn't
support control arrays) where you create one control and then copy the
same control multiple times. Control arrays are a form of inheritance
(but that is at the design level of VB which was developed in some
version of C). You can simulate control arrays in Access by creating a
textbox and then keep copying it (or just keep creating new textboxes)
and then naming the textboxes like txt0, txt1, txt2, ...

In a VB control array you can loop through the control like

For i = 0 to 5 : Debug.Print txtN(0).Text : Next

In Access you can do this:

Dim ctl As Control
For Each ctl In Me.Controls
If ctl.ControlType = acTextBox Then Debug.Print ctl.Name

Of course, for this example, the textboxes could be named anything. But
here is a better simulation of control arrays in Access

Dim i As Integer
For i = 0 To 4
Debug.Print Me.Controls("txt" & i).Name

Or another option would be to stuff the textboxes into a Collection
object. Then you can loop through the collection object

Dim col As New Collection, v As Variant
For i = 0 To 4: col.Add Me.Controls("txt" & i): Next
For Each v In col: Debug.Print v.Name: Next

Then only catch is that you have to know ahead of time how many
textboxes you have in Access where a VB control array knows
automatically. Of course, you could always just loop through the
controls collection of your form and anything that is a textbox gets
stuffed into the collection object (again, the textboxes could be named
anything in this case).


*** Sent via Developersdex ***
Don't just participate in USENET...get rewarded for it!
Nov 13 '05 #2

P: n/a
ra***** (RBohannon) wrote in message news:<ad************************** com>...
Is it possible to create a control array on an unbound form? I would
like to be able to loop through a series of unbound text boxes.


Nope. Access VBA <> normal VB, where you *can* have control arrays.
You have to name your controls similarly... ie

txtControl1, txtControl2, etc and then you can do something like this.

'warning - pathetic code ahead... but it *does* work

Private Sub Command6_Click()
Dim strControlName As String
Dim intCounter As Integer
strControlName = "Text"
For intCounter = 0 To 4 Step 2
Me.Controls(strControlName & intCounter).Value = intCounter
Next intCounter

End Sub

In my case I have Text0, Text2, Text4 as my textboxes...
Nov 13 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.