Solved

Initialize Control Array with Loop

Posted on 2000-03-07
8
182 Views
Last Modified: 2010-05-02
Experts,

Is it possible to loop through control names, ie textboxes with number extensions using a for loop?

For i = 14 To 37
  RowID = "Txtbox" & cstr(i)
  frmMain.RowID.Text = cstr(StoArray(i))
Next i

I'm trying to initialize textboxes using the array and this is not working?

How is this done?  Thank you.
0
Comment
Question by:vbdvlper
8 Comments
 
LVL 12

Accepted Solution

by:
roverm earned 40 total points
ID: 2591454
Yes:

For i = 14 To 37
  RowID = "Text" & CStr(i)
  frmMain.Controls(RowID).Text = "anynewvalue"
Next i

where all of your textboxes are numbered: Text14, Text15 and so on!

D'Mzzl!
RoverM
0
 
LVL 1

Expert Comment

by:North
ID: 2591475
Try This,

Dim i As Integer

For i = 14 To 37 Step 1

   frmMain.TextBox(i).Text = cstr(StoArray(i))

Next i


Hope this helps, North
0
 

Author Comment

by:vbdvlper
ID: 2591479
Thank you....can this loop be used to evaluate all controls on the form>
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 18

Expert Comment

by:deighton
ID: 2591487
The thing to do is create a control array, copy and paste an existing text box to your form.  VB will ask you if you want to create a control array i.e text(1),text(2)...text(n)  then reference real easy.

For i = 14 To 37

                    RowID = Text (i)

Next i
0
 
LVL 1

Expert Comment

by:North
ID: 2591495
Sorry RoverM, yours appeared as I was submitting mine.

vbdvlper, the difference between my code and RoverM's is mine relies on the textboxes being in a control array (i.e. same name property, different Index property). RoverM's doesn't need a control array, choose whichever you prefer.

North.
0
 
LVL 12

Expert Comment

by:roverm
ID: 2591518
Yes (again):

Dim ctl As Control

For Each ctl On frmMain
    If TypeOf ctl Is TextBox Then
      'do textbox stuff
    End If
Next ctl

D'Mzzl!
RoverM
0
 
LVL 12

Expert Comment

by:roverm
ID: 2591524
by the way: ctl is the reference to the current selected TextBox, so all the properties are available:

ctl.Text = "anyvalue"
ctl.Font = "Arial"

And so on!
RoverM
0
 
LVL 12

Expert Comment

by:roverm
ID: 2591526
North: no problem ;-)) ! RoverM
0

Featured Post

Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

This article describes some techniques which will make your VBA or Visual Basic Classic code easier to understand and maintain, whether by you, your replacement, or another Experts-Exchange expert.
Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

733 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question