Solved

Referring to controls with variables (30 pts)

Posted on 1998-12-11
4
163 Views
Last Modified: 2010-05-03
How can I refer to controls with a variable.E.g. I want to set the 'left' property of six text boxes to 100. Rather than type "text1.text = 100" for each (changing the number), how can I use one line in a repeat loop, where the number '1' in the above example is a variable that changes?
0
Comment
Question by:The_Oracle
  • 2
4 Comments
 
LVL 2

Accepted Solution

by:
BergJC earned 30 total points
ID: 1449513
For i = 1 To (Form1.Controls.Count - 1)
    If TypeOf Form1.Controls(i) Is TextBox Then
        Form1.Controls(i).Left = "100"
    End If
Next

Now, this will change all text boxes on your form. If that won't work for you, you'll have to put an extra check like this:

For i = 1 To (Form1.Controls.Count - 1)
    If TypeOf Form1.Controls(i) Is TextBox Then
        If Form1.Controls(i).Name = "Text1" or "Text2" Then
             Form1.Controls(i).Left = "100"
        End If
    End If
Next
0
 
LVL 13

Expert Comment

by:Mirkwood
ID: 1449514
Make a control array of text boxes:
So name all the text boxes "MyText" and set the index property to 0,1,2,3,4,5 etc
Now use a for loop
--
Dim t as textbox
for each t in MyText
   t.text = 100
next
--
or
--
for i = 0 to 6
   MyText(i).text = 100
next
0
 
LVL 2

Expert Comment

by:BergJC
ID: 1449515
Oops...made a mistake. That loop should start out with:

For i = 0 To (Form1.Controls.Count - 1)

0
 
LVL 3

Expert Comment

by:jbil
ID: 1449516
Dim objControl As Control

  For Each objControl In Me
    If TypeOf objControl Is TextBox Then
   
     objControl.Left = 100
     
    End If
  Next

0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering 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

Introduction While answering a recent question (http://www.experts-exchange.com/Q_27402310.html) in the VB classic zone, I wrote some VB code in the (Office) VBA environment, rather than fire up my older PC.  I didn't post completely correct code o…
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…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
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…

789 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