Solved

Referring to controls with variables (30 pts)

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

When trying to find the cause of a problem in VBA or VB6 it's often valuable to know what procedures were executed prior to the error. You can use the Call Stack for that but it is often inadequate because it may show procedures you aren't intereste…
Have you ever wanted to restrict the users input in a textbox to numbers, and while doing that make sure that they can't 'cheat' by pasting in non-numeric text? Of course you can do that with code you write yourself but it's tedious and error-prone …
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…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

910 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