Solved

Referring to controls with variables (30 pts)

Posted on 1998-12-11
4
162 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

Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Best way to parse out a json string in VB6? 10 174
VBA filters 2 62
Using an encrypted  value to decrypt and display contents in vb6 9 52
MsgBox 4 61
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.
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
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 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…

831 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