Hey All,
How do you split the following text from a text box...
MyText, andsomemoretext
...and put the second part into text2 textbox?
I'm using VB5, and cannot use the Split function.
Thanks in advance,
Ron
Printable View
Hey All,
How do you split the following text from a text box...
MyText, andsomemoretext
...and put the second part into text2 textbox?
I'm using VB5, and cannot use the Split function.
Thanks in advance,
Ron
Use the InStr function to check for the instance of the dilimiter.
Then, is InStr returns greater than 0 use the Left$() or Right$() string functions to get all the text before (or after depending on which one you use).
And that should do it :)
Cheers,
RyanJ
try something like this.
VB Code:
Private Sub Command1_Click() Dim strFirstString As String strFirstString = "MyText, andsomemoretext" Dim x As String Dim y As String x = Left$(strFirstString, InStr(1, strFirstString, ",") - 1) y = Trim$(Mid$(strFirstString, InStr(1, strFirstString, ",") + 1, _ (Len(strFirstString) - (InStr(1, strFirstString, ","))))) text1.Text = x text2.Text = y End Sub
Hope this helps!!
:wave: :thumb: :wave:
You can use the split command like tihs:
VB Code:
Text2.Text = Split(Text1, ",")(1) Text1.Text = Split(Text1, ",")(0)
Pradeep :)
EDIT: The above code is assuming that your Text1 contains the string you want to split.
One more way (using split and control arrays).
-Place a textbox on the form, name it Text1, set its index property to 0.
-Place another texbox and name it Text2.
-Place a command button and name it Command1.
-Paste the code in the form
-Run the application
-Enter the text (delimited with commas) in Text2
-Press the command button
VB Code:
Option Explicit Private Sub Command1_Click() Dim strBuffer() As String Dim I As Long strBuffer = Split(Text2.Text, ",") For I = LBound(strBuffer) To UBound(strBuffer) If I <> 0 Then Load Text1(I) Text1(I).Text = Trim$(strBuffer(I)) Text1(I).Top = Text1(I - 1).Top + Text1(I).Height Text1(I).Visible = True Else Text1(I).Text = Trim$(strBuffer(I)) End If Next I End Sub
Thanks for the replies fellers. :)
The solution that Bombdrop provided worked very well. The input of
sciguyryan explained it to me, and the solutions of baja_yu, and Pradeep1210
use the Split function which I can't use because I'm using VB5.
Thanks to all of you for your help! :)
Here is one that does it without the split function so you can use it. It works with more than two items.
VB Code:
Option Explicit Private Sub Command1_Click() Dim strBuffer As String Dim intPass As Long intPass = 0 Do If InStr(Text2.Text, ",") > 0 Then strBuffer = Left$(Text2.Text, InStr(Text2.Text, ",") - 1) Text2.Text = Right$(Text2.Text, Len(Text2.Text) - InStr(Text2.Text, ",")) Else strBuffer = Text2.Text Text2.Text = "" End If If intPass <> 0 Then Load Text1(intPass) Text1(intPass).Text = Trim$(strBuffer) Text1(intPass).Top = Text1(intPass - 1).Top + Text1(intPass).Height Text1(intPass).Visible = True Else Text1(intPass).Text = Trim$(strBuffer) End If intPass = intPass + 1 Loop Until InStr(Text2.Text, ",") = 0 If Len(Text2.Text) > 0 Then Load Text1(intPass) Text1(intPass).Text = Trim$(Text2.Text) Text1(intPass).Top = Text1(intPass).Top + Text1(intPass).Height Text1(intPass).Visible = True Text2.Text = "" End If End Sub
It works Fine men
There is a Split function for VB5 in Codebank, and also on MSDN Knowledge Base there is a page with Split and Replace functions for VB5.
Here's one by abhijit.