How to set a textbox as a object in vb6

How do you set a textbox as a object in VB6.

I want to be able do a loop that fills text boxes on a form.

Something like this:

Dim Tbox as object
BoxNum=2
Box="text"
while BoxNum < 20
set Tbox=Box & BoxNum
Tbox.value= BoxNum
BoxNum =BoxNum+1
wend

My problem is I don't know how to assing a object as a textbox or if that is what I should be doing.









whiwexAsked:
Who is Participating?
 
JohnBPriceConnect With a Mentor Commented:
In VB6 you can't easily refer to a control by a dynamically built name like "textBox" +1.  You can, however, prowl through the list of controls to find the name, e.g.

Function GetTextBox(ControlNameToFind As String) As Control
    Dim ctl As Control
    For Each ctl In Me.Controls
        If ctl.Name = ControlNameToFind Then
            Set GetTextBox = ctl
            Exit Function
        End If
    Next
    Set GetTextBox = Nothing
End Function


so you can do things like
GetTextBox("Text" & 1).text = "Hi There"
Note however my example does NOT support arrays of controls, which have the same name but a different index value, although you could easily enhance it.
0
 
Jim HornMicrosoft SQL Server Developer, Architect, and AuthorCommented:
>set Tbox=Box & BoxNum
>Tbox.value= BoxNum

You should also be able to accomplish this with...

Me("text" & BoxNum).Value = BoxNum
0
 
Jim HornMicrosoft SQL Server Developer, Architect, and AuthorCommented:
or even better.. use a control array, then you can use..

MyTextBoxName(BoxNum) = BoxNum
0
Upgrade your Question Security!

Your question, your audience. Choose who sees your identity—and your question—with question security.

 
Jim HornMicrosoft SQL Server Developer, Architect, and AuthorCommented:
Actually, VB uses .Text instead of .Value as the propertie that stores its value.  Access uses .Value.
0
 
vb_elmarCommented:
Dim Tbox As TextBox

For Each Tbox In Form1
    Tbox.Text = "Hello"
Next
0
 
vb_elmarCommented:
Dim Tbox

For Each Tbox In Form1
    If TypeOf Tbox Is TextBox Then Tbox.Text = "Hello"
Next
0
 
whiwexAuthor Commented:
No matter what I try I get errors.
0
 
vb_elmarCommented:
Is your form's name Form1 ?
0
 
whiwexAuthor Commented:
I get Type mismatch with what ever I try to set the TBox to
0
 
vb_elmarCommented:
Are you using VB6 ?


****************************************************************
Make a new Project. Add three Textboxes to Form1 (Text1 Text2 Text3).

Dim Tbox

For Each Tbox In Form1
    If TypeOf Tbox Is TextBox Then Tbox.Text = "Hello"
Next
****************************************************************
0
 
whiwexAuthor Commented:
elmar

i can do this.
What I want to be able to fill certain text boxes using a variable. Like
textboxX = something
textboxX+1 = something

thanks
0
 
JohnBPriceCommented:
Also note that it is case sensitive unless you include "option compare text" at the top of your form code
0
 
vb_elmarCommented:
Add three Textboxes to Form1 (Text1 Text2 Text3).


Private Sub Form_Load()
    textboxX 1, "something 1"
    textboxX 2, "something 2"
End Sub


Sub textboxX(ByVal z As String, ByVal myText As String)
    Dim Tbox
    For Each Tbox In Form1
        If TypeOf Tbox Is TextBox Then
                    If Tbox.Name = "Text" & z Then Tbox.Text = myText
        End If
    Next
End Sub
0
 
vb_elmarCommented:
Add 20 Textboxes to Form1 (Text1 Text2 Text3... ).

Private Sub Form_Load()
    While BoxNum < 20
        BoxNum = BoxNum + 1
        textboxX BoxNum, "something " & BoxNum
    Wend
End Sub


Sub textboxX(ByVal z As String, ByVal myText As String)
    Dim Tbox
    For Each Tbox In Form1
        If TypeOf Tbox Is TextBox Then
                    If Tbox.Name = "Text" & z Then Tbox.Text = myText
        End If
    Next
End Sub
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.