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

Concatenate a control name using Code?

Status
Not open for further replies.

MattNMNB

Technical User
Sep 25, 2001
7
US
Is it possible to "build" a control name reference using code. For Example,
I have a form with 80 text boxes on it. They are named txtType1, txtType2, txtType3 ,
....., txtType80.

I want to construct a loop to process the various properties of the text boxes. I have tried various things to no avail. I am thinking something like the following:

Private Sub cmdUpdate_Click()
Dim counter As Integer
Dim varType As Stringl

counter = 1

While counter < 81
varType = &quot;txtType&quot; & counter
Me.Controls(varType).text = (Something)
(Set other properties here)

counter = counter + 1
Wend

End Sub

This is not working but it gives a sense of the logic I am trying to use. Any help would be appreciated.

MattB
 
Hi Matt!

I don't see anything wrong with your concatenation, though you should probably use the format command:

varType = &quot;txtType&quot; & format(counter)

What you are doing looks to be better suited to a For-Next loop:

For counter = 1 To 80
your stuff
Next counter

The only other problem I see (which doesn't look like a typo such as Stringl) is that you cannot access the Text property of a text box without setting the focus to the box first. But, if that were the problem, you should be getting an error, Property is not available at this time, or something like that. Anyway, I would change the .text to .value.

hth
Jeff Bridgham
 
Jeff,

Thank you very much for your help. The addition of the Format command in the concatenation did the trick. It is working now. I will also change the looping construct to a For Next loop. You were also correct about the .text property not beign accessible without the control having the focus. I am now using the .value property.
Thanks again for your help.

Matt
 
As an alternative, you could just itterate through the controls collection and check the control name pattern for a match to your control elements. If the pattern is a match, get the 'stuff', else just move on (to the next control). The advantage is that adding/deleting modifying controls does not afect the code, the downside is that you would 'investigate' every control on the form.

While you appear to satisfied with the soloution, I went forth and generated a pseudo code procedure along theis line, and provides it free of charge (and testing) in case others have a similar issues.

Code:
Private Sub cmdUpdate_Click()

    Dim strCtrlName As String
    Dim intPattLen As Integer
    Dim MyCtrl As Control
    Dim strCtrlPatern As String

    strCtrlPatern = &quot;txtType&quot;
    intPattLen = Len(strCtrlPatern)

    For Each MyCtrl In Me.Controls
        strCrtlName = MyCtrl.Name
        If (Left(strCtrlName, intPattLen) = strCtrlPatern) Then

            '(Set other properties here)
        End If

    Next MyCtrl

End Sub
MichaelRed
m.red@att.net

There is never time to do it right but there is always time to do it over
 
MichaelRed,

That is also a very good solution. And one would suspect that with 80 text boxes on one form, there would not be much room for other controls! :)

Jeff Bridgham
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top