Solved

Initialize Control Array with Loop

Posted on 2000-03-07
8
180 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
Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
VB error "Type mismatch" 2 60
Copy a range from 1..n excel sheets to one destination sheet 2 56
VB6 common control 6 sp6 object library not registerd 3 22
Problem to line 23 54
I’ve seen a number of people looking for examples of how to access web services from VB6.  I’ve been using a test harness I built in VB6 (using many resources I found online) that I use for small projects to work out how to communicate with web serv…
Article by: Martin
Here are a few simple, working, games that you can use as-is or as the basis for your own games. Tic-Tac-Toe This is one of the simplest of all games.   The game allows for a choice of who goes first and keeps track of the number of wins for…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

856 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