Link to home
Start Free TrialLog in
Avatar of whiwex
whiwex

asked on

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.









Avatar of Jim Horn
Jim Horn
Flag of United States of America image

>set Tbox=Box & BoxNum
>Tbox.value= BoxNum

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

Me("text" & BoxNum).Value = BoxNum
or even better.. use a control array, then you can use..

MyTextBoxName(BoxNum) = BoxNum
Actually, VB uses .Text instead of .Value as the propertie that stores its value.  Access uses .Value.
Dim Tbox As TextBox

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

For Each Tbox In Form1
    If TypeOf Tbox Is TextBox Then Tbox.Text = "Hello"
Next
Avatar of whiwex
whiwex

ASKER

No matter what I try I get errors.
Is your form's name Form1 ?
Avatar of whiwex

ASKER

I get Type mismatch with what ever I try to set the TBox to
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
****************************************************************
Avatar of whiwex

ASKER

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
ASKER CERTIFIED SOLUTION
Avatar of JohnBPrice
JohnBPrice

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Also note that it is case sensitive unless you include "option compare text" at the top of your form code
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
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