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 SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

just being head strong but thats the way to learn... 2

Status
Not open for further replies.

Almarton

Programmer
Jan 1, 2002
42
0
0
BR
Hi all,

I have a vb code doing assignments to 100 text fields
like:

txtField1.Text = strCod

'... to ...

txtField100.Text = strCod


Is there a way to do a loop on those assignments
instead of writing 100 lines? I thought about
concatenating "txtField" + counter + ".Text = strCod"
but it would not be recognized as a command line code?
Can any of you pros find out this one? Or some alternative?

Thanks in advance!
Alexandre Marton
Almarton (BRAZIL)






Alexandre @lmarton Marton
almarton@task.com.br
 
You could maybe loop through the Controls collection?

Code:
Dim ctrl As Control
    
For Each ctrl In Me.Controls
    If TypeOf ctrl Is TextBox Then
        ctrl.Text = strCod
    End If
Next

The above code would set the text of ALL TextBoxes on the Form to strCod, but this could be a starting point for you.

--
Jonathan
 
You could also try a control array.

For i = Text1.LBound To Text1.UBound
Text1(i).Text = "TestIt"
Next

Swi
 
Ok , both answers are good though the code is extense and the text fields are not in array form. Also I cannot afford to change the text fields to array form (only if it was the only and last solution), so is there a way to execute a concatenated string as a VB command? Lets re-state:

"txtField" + counter + ".Text = someOtherVariable"

'where counter goes inside a for from 1 to 100


and after execute this string as a regular command line code (meaning: txtField.1.Text = someOtherVariable )



Alexandre @lmarton Marton
almarton@task.com.br
 
From where is coming the
= someOtherVariable
because your problem is not a loop for the Controls. Your problem is to have a sistem to creat a algorithm of your sources.

Peter Guhl
Chile


peterguhl@yahoo.de
 
How about this idea...

Code:
For i = 1 To 100
   Controls("txtField" & i).Text = "Blah"
Next i

--
Jonathan
 
This is just modified code from above:

Dim ctrl As Control
Dim strCod As String
Private Sub Form_Load()
strCod = "TestIt"
For Each ctrl In Me.Controls
If TypeOf ctrl Is TextBox And Left(ctrl.Name, 4) Like &quot;Text&quot; And Mid$(ctrl.Name, 5) < 101 Then
ctrl.Text = strCod
End If
Next
End Sub

Swi
 
Along with the Control loop, you could just add your own &quot;Index&quot; or ID to the control's Tag property at design time.

Then in the loop:
Select Case ctrl.Tag
Case 0
ctrl.Text = strCod(ctrl.Tag) 'An Array of data
'Or just a string
'ctrl.Text = strCod
Case 1
.
.
.
End Select

OR, to permanently (Run time) link a control to a certain VARIABLE, use the variable name in the Tag property:

Select Case UCase$(ctrl.Tag)
Case &quot;STRCOD&quot;
ctrl.Text = strCod
Case 1
.
.
.
End Select

 
Great solution SWI, I can only say (Cartman way)

- THAT DOES IT!

you rule man.
Thanks.

Alexandre @lmarton Marton
almarton@task.com.br
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top