We help IT Professionals succeed at work.

Using MyReader As New Microsoft.VisualBasic.FileIO.TextFieldParser

RGuillermo asked
Last Modified: 2012-05-29
Hello experts,
The following sub works justfine the first time its called by the main program.
The second time it stop without giving any message.
it stops when trying to execute :

MyReader.TextFieldType = FileIO.FieldType.Delimited

Is it not possible to open the same text file twice in the same program?
If each time I am opening and closing properly?

END USING is this sentence closing properly the file? or I need to do something else?
see sub below.

    Private Sub s_load_grades(ByVal p_per_num)
        Dim currentrow(150) As String, currentfield As String, r As Integer
        Dim a(25) As String, n As Integer, t1 As Integer, t2 As Integer
        t1 = a_per_nnn(p_per_num, 4)   ' lo que tiene que avanzar antes de hallar la data correcta.
        t2 = a_per_nnn(p_per_num, 3)      ' lo que tiene que leer una vez hallada la data correcta.

        Using MyReader As New Microsoft.VisualBasic.FileIO.TextFieldParser(ss_tmp_fdr & "tmp_gra.txt")
            MyReader.TextFieldType = FileIO.FieldType.Delimited
            r = 0
            While Not MyReader.EndOfData And r <= t1 And t1 > 0
                r += 1
                currentrow = MyReader.ReadFields()
            End While
            While Not MyReader.EndOfData And r >= t1 And r <= t1 + t2
                r += 1
                    currentrow = MyReader.ReadFields()
                    n = 0 'MsgBox(currentrow)
                    For Each currentfield In currentrow
                        n += 1
                        a_gra(r, n) = currentfield
                Catch ex As Microsoft.VisualBasic.FileIO.MalformedLineException
                    MsgBox("Line " & ex.Message & "is not valid and will be skipped.")

                End Try
            End While

        End Using

    End Sub
Watch Question

Most Valuable Expert 2011
Top Expert 2015

What do you mean by, "it stop"?
RGuillermoProject Manager


I mean the program simply wait for me to use the active form.
it should finish reading the file or give me at least a message but nothing.
it simply focuses the form and waits.
Mike TomlinsonHigh School Computer Science, Computer Applications, Digital Design, and Mathematics Teacher
Top Expert 2009

How much time has transpired between calls?...it may be that the file hasn't been fully released since the last execution.
RGuillermoProject Manager


Yes I think so. Between calls it been just few seconds....
Is there any command to close the file properly.

The sub itself should close the file. Why is it not being closed?
Mike TomlinsonHigh School Computer Science, Computer Applications, Digital Design, and Mathematics Teacher
Top Expert 2009

The using block should close the file.  It does take a little bit of time for the operating system itself to fully release the file.

It could be something causing the problem that we can't see.  Can you post more code or explain how the program is used and how it flows?
RGuillermoProject Manager


The program is very small. It has to read some text files put the data into several excell sheets in the same book.
Most text files are read several times as you can see in the main loops. No one has problems except this "s_load_grades" sub, that opens the file  "tmp_gra.txt".  After reading correctly the first time the next it simply does nothing, stops excecution and sends focus to the form.

Here you have the attachment of the project with the text files. Please place the text files in folder c:\tmp\
High School Computer Science, Computer Applications, Digital Design, and Mathematics Teacher
Top Expert 2009
This one is on us!
(Get your first solution completely free - no credit card required)
RGuillermoProject Manager


I tried with more than two secs.. still the same problem...
Is there any way to clear memory or close all open files in vb10?
RGuillermoProject Manager


Problem solved, your advice helped me realize additionally I had a problem with an array, I tried to assign a value to a row that does not exist. Rare the vb10 would not give an error message! but solved anyway. Than you.

Gain unlimited access to on-demand training courses with an Experts Exchange subscription.

Get Access
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Empower Your Career
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE

Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Unlock the solution to this question.
Join our community and discover your potential

Experts Exchange is the only place where you can interact directly with leading experts in the technology field. Become a member today and access the collective knowledge of thousands of technology experts.

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.


Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.