Link to home
Get AccessLog in
Avatar of RGuillermo
RGuillermoFlag 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.

    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
Avatar of kaufmed
Flag of United States of America image

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


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.
How much time has transpired between calls? may be that the file hasn't been fully released since the last execution.
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?
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?
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\
Avatar of Mike Tomlinson
Mike Tomlinson
Flag of United States of America image

Link to home
This content is only available to members.
To access this content, you must be a member of Experts Exchange.
Get Access
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?
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.