Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 285
  • Last Modified:

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.









0
whiwex
Asked:
whiwex
  • 6
  • 3
  • 3
  • +1
1 Solution
 
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
 
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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
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:
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
 
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

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 6
  • 3
  • 3
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now