Solved

Referring to controls with variables (30 pts)

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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

If you have ever used Microsoft Word then you know that it has a good spell checker and it may have occurred to you that the ability to check spelling might be a nice piece of functionality to add to certain applications of yours. Well the code that…
I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
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…

708 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now