Re: How to read each line from multiline textbox?
Even if we saw the project we probably couldn't help much as we couldn't send him the fixed version unless we had VB5 ourselves. I think we've given him the answer he needs to fix it though, so hopefully he'll make the changes and resolve/rate :-)
Edit: Maybe not...split function is fine in module1, but if the code you posted is in the main form adding the form name won't make a difference...there must be something else causing the issue
Re: How to read each line from multiline textbox?
Form1
VB Code:
Private Sub Command1_Click()
Dim strArray() As String
strArray = Split(txtText, vbCrLf)
Text1.Text = strArray(0)
Text2.Text = strArray(1)
Text3.Text = strArray(2)
End Sub
Module1
VB Code:
Public Function Split(ByVal sIn As String, _
Optional sDelim As String = " ", _
Optional nLimit As Long = -1, _
Optional bCompare As VbCompareMethod = vbBinaryCompare) _
As Variant
Dim nC As Long, nPos As Long, nDelimLen As Long
Dim sOut() As String
If sDelim <> "" Then
nDelimLen = Len(sDelim)
nPos = InStr(1, sIn, sDelim, bCompare)
Do While nPos
ReDim Preserve sOut(nC)
sOut(nC) = Left(sIn, nPos - 1)
sIn = Mid(sIn, nPos + nDelimLen)
nC = nC + 1
If nLimit <> -1 And nC >= nLimit Then Exit Do
nPos = InStr(1, sIn, sDelim, bCompare)
Loop
End If
ReDim Preserve sOut(nC)
sOut(nC) = sIn
Split = sOut
End Function
Re: How to read each line from multiline textbox?
When you run the code and it crashes, can you find out what line it crashes at? I've never used VB5 but in VB6, it gives you the option to debug which will show you the line where the error is
Re: How to read each line from multiline textbox?
You need to place error handling in your Split function as if it errors code execution will be thrown back to the calling procedure - Command1_Click().
Re: How to read each line from multiline textbox?
it highlights the following
strArray =
and gives me the error "Can't assign to array"
Re: How to read each line from multiline textbox?
Tell you what, here's what I'll do....I'll write a custom function you can use instead of using split...it's a simple matter to do a little parsing. I'll edit this post with it if no-one replies :-)
Re: How to read each line from multiline textbox?
The error could be because in the Split function your second argument is a string whereas vbCrLF is a combination of these two characters Chr$(13)+Chr$(10).
Change it to this...
VB Code:
strArray = Split(txtText, "vbCrLf")
Re: How to read each line from multiline textbox?
Quote:
Originally Posted by RobDog888
Change it to this...
Tell me if that works...if not, I'll code the function :-)
Re: How to read each line from multiline textbox?
Actually I dont think it will work still as it gets evaluated later in the procedure.
Re: How to read each line from multiline textbox?
no it doesn't work.....same error
thanks smUX
edit:
thanks to all you guys for helping :-)
Re: How to read each line from multiline textbox?
This code relies on instr() and mid() and this is my test code as before with modifications...took a bit of tweaking but it works. Hopefully other people will read and comment on a better way to do it in VB5 or perhaps point out stuff that could be changed/tweaked :-)
VB Code:
Private Sub Command1_Click()
Dim strArray() As String, startof(2) As Single, endof(2) As Single
startof(0) = 1: endof(0) = InStr(1, txtText, vbCrLf)
startof(1) = endof(0) + 2: endof(1) = InStr(startof(1), txtText, vbCrLf)
startof(2) = endof(1) + 2: endof(2) = InStr(startof(2), txtText, vbCrLf)
Text1.Text = Mid(txtText, startof(0), endof(0) - startof(0))
Text2.Text = Mid(txtText, startof(1), endof(1) - startof(1))
Text3.Text = Mid(txtText, startof(2), Len(txtText) - startof(2) + 1)
End Sub
Private Sub Form_Load()
txtText = "a" & vbCrLf & "b" & vbCrLf & "c"
End Sub
Re: How to read each line from multiline textbox?
Hey thanks smUX.....that works! :)
Re: (Resolved) How to read each line from multiline textbox?
Re: (Resolved) How to read each line from multiline textbox?
Hey smUX,
You ought to post this code in the codebank.....both versions VB5, and VB6,
and maybe how to load the multiline textbox from the textboxes.
VB Code:
txtText = Text1.Text & vbCrLf & Text2.Text & vbCrLf & _
Text3.Text & vbCrLf & Text4.Text
Thanks again,
Ron