Avatar of RGuillermo
RGuillermo
Flag for United States of America asked on

Using MyReader As New Microsoft.VisualBasic.FileIO.TextFieldParser

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.
Regards

    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
            MyReader.SetDelimiters(",")
            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
                Try
                    currentrow = MyReader.ReadFields()
                    n = 0 'MsgBox(currentrow)
                    For Each currentfield In currentrow
                        n += 1
                        a_gra(r, n) = currentfield
                    Next
                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
Visual Basic.NET

Avatar of undefined
Last Comment
RGuillermo

8/22/2022 - Mon
kaufmed

What do you mean by, "it stop"?
RGuillermo

ASKER
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 Tomlinson

How much time has transpired between calls?...it may be that the file hasn't been fully released since the last execution.
Your help has saved me hundreds of hours of internet surfing.
fblack61
RGuillermo

ASKER
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?
Regards,
Mike Tomlinson

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?
RGuillermo

ASKER
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\
rep-b7-rep-b7-xls-net.zip
Get an unlimited membership to EE for less than $4 a week.
Unlimited question asking, solutions, articles and more.
ASKER CERTIFIED SOLUTION
Mike Tomlinson

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question
RGuillermo

ASKER
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?
regards
RGuillermo

ASKER
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.