Solved

Referring to controls with variables (30 pts)

Posted on 1998-12-11
4
165 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

Question has a verified solution.

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

The debugging module of the VB 6 IDE can be accessed by way of the Debug menu item. That menu item can normally be found in the IDE's main menu line as shown in this picture.   There is also a companion Debug Toolbar that looks like the followin…
You can of course define an array to hold data that is of a particular type like an array of Strings to hold customer names or an array of Doubles to hold customer sales, but what do you do if you want to coordinate that data? This article describes…
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…

726 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