Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Shaun E on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Substring error

Status
Not open for further replies.

josie2007

Technical User
Apr 14, 2007
90
US
I am getting Substr is not declared error ...any clue


Do While objReader.Peek() <> -1
L = Len(TextLine)
TextLine = TextLine & objReader.ReadLine() & vbNewLine
I = Substr(LTrim(TextLine), 1, 6))
If (I = "ITEM01") Then
MsgBox("ITEM01 Has been found")
End If

Loop
TextBox1.Text = TextLine
 
Hi,

I've never used "Substr" but rather the String."Substring" function.

You could try:

Code:
       LTrim(YourString).Substring(startindex, length)
 
Mastakilla is correct.

I don't think the old SubStr function exists in .NET.

At least not without digging into the VB6 compatibility classes... I know it's tempting, but don't do it! LOL!


Senior Software Developer
 
Thank you for the response.I think that will work for me thank you. I am getting System.IO.StreamReader included in my output and I attached my output at the end...please let me know where I made a mistake. thanks


Dim ebsFiles As String = "C:\Expedite\gen\pass2ebs.txt"
Dim objReader As New System.IO.StreamReader(ebsFiles)
Dim TextLine As String = objReader.ToString

Do While objReader.Peek() <> -1
L = Len(TextLine)
TextLine = TextLine & objReader.ReadLine() & vbNewLine
I = LTrim(TextLine).Substring(1, 6)
If (I = "ITEM01") Then
MsgBox("ITEM01 Has been found")
End If

Loop



System.IO.StreamReaderEBS Pass File, 3.00,
LETTING01,04/27/07,9:30 A.M.
LETTING02,
LETTING03,"MN/DOT, STPAUL"
LETTING04,
LETTING05,,
 
Just FYI, the equivalent to Substr in .NET is the Mid function, which is a holdover from VB 6. The Mid function works using exactly the syntax you used when trying to do the Substr function.





I used to rock and roll every night and party every day. Then it was every other day. Now I'm lucky if I can find 30 minutes a week in which to get funky. - Homer Simpson

Arrrr, mateys! Ye needs ta be preparin' yerselves fer Talk Like a Pirate Day!
 
Thank for the clarification, I've never used VB6 hence my biaised deduction.
 
Any clue why it is only read the first line from the text file? thanks


Do While objReader.Peek() <> -1
' Dim I As String = ""
' L = Len(TextLine)
TextLine = TextLine & objReader.ReadLine() & vbNewLine
I = LTrim(TextLine).Substring(0, 6)
If (I = "ITEM01") Then
MsgBox("ITEM01 Has been found")
End If

Loop
 
thanks for the response...but it is still reading the first line only "EBS Pa" ...I do not know why? below is the sample of text file I used


EBS Pass File, 3.00,
LETTING01,04/27/07,9:30 A.M.
LETTING02,
LETTING03,"MN/DOT, STPAUL"
LETTING04,
LETTING05,,
 
Here is the code I use to read in a file.

Code:
Public Function read_file(ByVal FileLocation As String) As Collection
        Dim sr As StreamReader
        Dim LineList As New Collection

        If File.Exists(FileLocation) = True Then
            sr = New StreamReader(FileLocation)

            Do While Not sr.EndOfStream
                LineList.Add(sr.ReadLine)
            Loop
            sr.Close()
        End If

        Return LineList
End Sub
I do it this way so it is totally generic and I can then do whatever I want with it after it is pulled in, but you could do:

Code:
        Dim sr As StreamReader
        Dim LineList As New Collection

        If File.Exists(FileLocation) = True Then
            sr = New StreamReader(FileLocation)

            Do While Not sr.EndOfStream
                TextLine = TextLine & sr.ReadLine
                I = LTrim(TextLine).Substring(0, 6)
                If (I = "ITEM01") Then
                    MsgBox("ITEM01 Has been found")
                End If

            Loop
            sr.Close()
        End If

End Sub


-I hate Microsoft!
-Forever and always forward.
 
Sorry forgot to remove the Dim LineList as New Collection as well.

-I hate Microsoft!
-Forever and always forward.
 
To use your code more exactly:

Code:
        Dim ebsFiles As String = "C:\Expedite\gen\pass2ebs.txt"
        Dim objReader As New System.IO.StreamReader(ebsFiles)
        Dim TextLine As String = objReader.ToString

        Do While Not objReader.EndOfStream
            L = Len(TextLine)
            TextLine = TextLine & objReader.ReadLine() & vbNewLine
            I = LTrim(TextLine).Substring(1, 6)
            If (I = "ITEM01") Then
                MsgBox("ITEM01 Has been found")
            End If
        Loop

Though now that I think about it I've never tried "= objReader.ToString" so I have to clue what effect if any it has on the whole process.

-I hate Microsoft!
-Forever and always forward.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top