• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1005
  • Last Modified:

Text Box using instr, mid, left function

I have 4 text boxes
Text1.Text = Joe Sam Doe
I need the other 3 text boxes to show 1 word per text box LIKE:

Text2.Text = Joe
text3.text = Sam
text4.text = Doe

I guessing mid or instr function might work but I'm not sure how to do it.
If their is a better or faster way to do it, please post it.

I am using Visual Basic 6 pro.

  • 2
1 Solution
Jorge PaulinoIT Pro/DeveloperCommented:
Hi daneburr,

Try this way:

Dim str() As String

str = Split(Text1.Text, " ")
Text2.Text = str(0)
Text3.Text = str(1)
Text4.Text = str(2)
or if you want to use left/mid

Dim a As Integer, b As Integer

'//This will find the location of the first space in Text1 (the one after Joe)
'  and add 1 to get it past the space.
a = InStr(1, Text1.Text, " ") + 1

'//This will find the location of the second space in Text1 (the one after Sam)
b = InStr(a, Text1.Text, " ")

'//This will take all the characters before the first space, leaving only Joe in Text2
Text2.Text = Left(Text1.Text, a - 2)

'//This will take all the characters between the two spaces, leaving just Sam in Text3
Text3.Text = Mid(Text1.Text, a, b - a)

'//This will get all the characters after the second space, leaving just Doe in Text4
Text4.Text = Mid(Text1.Text, b + 1)
Jorge PaulinoIT Pro/DeveloperCommented:
Thanks for the grade.

You can use this for validations UBound(str). This displays the size of the array.Don't forget that the arrays are zero based.

If you need to check the values of your array you can do this:
Dim x As Integer
For x = 0 to UBound(str) -1
  Msgbox str(x)
Next x
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.

Join & Write a Comment

Featured Post

The 14th Annual Expert Award Winners

The results are in! Meet the top members of our 2017 Expert Awards. Congratulations to all who qualified!

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now