Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Textbox names and code

Status
Not open for further replies.

sbeltyukov

Technical User
Jan 24, 2007
8
0
0
US
Hi,

If I have textboxes named say txtr1, txtr2, txtr3, etc. etc. I have the first part "txtr" the same for every box but the only thing that changes is the number. I defined an integer x as a counter and I want to populate the text box values as such:

Code:
txtr & x = "some text"

So I want to create a while loop that will loop through until x is less than say 10 and populate the text box whose number corresponds to the number of the counter. So I was thinking something like this:

Code:
While (x<10)
txtr & x = "some text"
x = x + 1
Wend

So if x = 1, then when the program enters into the loop, text box with the name txtr1 will get "some text" as its value, if x = 2 when the program is in the while loop then txtr2 will be populated by "some text", etc. etc. Is this possible?

Thank you!
 
Start a new project for this test. You will need 4 text boxes (Text1, Text2, Text3, Text4) and a command button (Command1). The paste the following code, run, and click on the command button
[tt]
Option Explicit

Private Sub Command1_Click()
Dim C As Control, I As Integer

I = 1

For Each C In Me.Controls

If TypeOf C Is TextBox Then
If C.Name = "Text1" Then
C.Text = "Found 1"
ElseIf C.Name = "Text2" Then
C.Text = "Found 2"
ElseIf C.Name = "Text3" Then
C.Text = "Found 3"
ElseIf C.Name = "Text4" Then
C.Text = "Found 4"
End If
End If

Next
End Sub
[/tt]

Good Luck
 

While (x<10)
Me.Controls("txtr" & Cstr(x)).Text = "some text"
x = x + 1
Wend



Better would be to use a control array

Add a text box (Text1) and change it's Index property to 0
Copy the text box and paste it again onto the form. The new Text box should have an index of 1.

Then:

While (x<2)
Text1(x).Text = "some text"
x = x + 1
Wend
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top