Results 1 to 6 of 6

Thread: TextFieldParser question

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Apr 2010
    Posts
    478

    TextFieldParser question

    I am learning to use TextFieldParser to read a fixed width text data file.
    It works great. I copied code below.
    Question: How to stop at the end of each line because I need to add some code?

    Using tf As New TextFieldParser(fileName)
    tf.TextFieldType = FileIO.FieldType.FixedWidth
    tf.SetFieldWidths(60, 30, 20) //three columns

    Dim row As String()
    While Not tf.EndOfData
    row = tf.ReadFields()
    For Each field As String In row
    MessageBox.Show(field)
    //need to add code in the end of each record, how?
    Next

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: TextFieldParser question

    I don't really understand what you mean by "stop" at the end of each line. Do you mean you want to get some input from the user after each line? Please provide a clearer explanation of exactly what you're trying to achieve.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3

    Thread Starter
    Hyperactive Member
    Join Date
    Apr 2010
    Posts
    478

    Re: TextFieldParser question

    Sorry, here is more detail.

    Dim row As String()
    While Not tf.EndOfData
    row = tf.ReadFields()
    For Each field As String In row
    MessageBox.Show(field)
    //need to add code in the end of each record, how?
    Next

    After code above, data will show like below:

    company1, location1, name1 (end of first record, I need to output to file1)
    company2, location2, name2 (end of second record, I need to output to file2)
    ...
    company10, location10, name10 (end of tenth record, I need to output to file10)

    I need to determine where is the end of each record so that I can sent output file.
    Last edited by aspfun; Aug 26th, 2010 at 07:55 AM.

  4. #4
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: TextFieldParser question

    So what you're actually saying is that you need to output each row to a separate file, correct? If so then that's exactly what you do. You simply add code inside the loop that writes the current row to a file.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  5. #5

    Thread Starter
    Hyperactive Member
    Join Date
    Apr 2010
    Posts
    478

    Re: TextFieldParser question

    Thank you.
    Another question:
    In order to get the value for each cell in the same row, I use code below using array.
    Is it possible to do the same job from TextFieldParser only?
    Dim Fields() As String
    While Not tf.EndOfData
    Fields = tf.ReadFields()
    s1 = Fields(0)
    s2 = Fields(1)
    s3 = Fields(2)
    End While

  6. #6
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: TextFieldParser question

    Evereything a TextFieldParser can do is in the documentation for the TextFieldParser class. If you want to read field values then that's how you do it.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width