Link to home
Start Free TrialLog in
Avatar of Bob Lamberson
Bob LambersonFlag for United States of America

asked on

file is already open vb 6 text file

testing the solution in https://www.experts-exchange.com/questions/21197487/error-'FIle-is-already-open'-vb6-text-file.html  I still get the file is already open error - any ideas why?

Public Sub WriteToTxt(strTable As String, intField As Integer, strFind As String, strReplace As String)
    Dim intFileHandle As Integer
    Dim intFileHandle2 As Integer
    Dim strRETP As String
    Dim FieldArray As Variant
    Dim found As Boolean

    found = False
   intFileHandle = FreeFile
    Open "C:\" & strTable & ".txt" For Input As #intFileHandle
    intFileHandle2 = FreeFile
    Open "C:\" & strTable & ".txt.tmp" For Output As #intFileHandle2 ' temp file
   
'*** error 'FIle is already open' in next line *******
    Open "C:\" & strTable & ".txt.tmp" For Output As #intFileHandle2 ' temp file
    While Not EOF(intFileHandle)
        Line Input #intFileHandle, strRETP
        If Not found Then
            FieldArray = Split(strRETP, "|")
            If FieldArray(0) = strFind Then
                FieldArray(intField) = strReplace
                strRETP = Join(FieldArray, "|")
                found = True
            End If
        End If
        Print #intFileHandle, strRETP
    Wend
    Close #intFileHandle
    Close #intFileHandle2
    Kill "C:\" & strTable & ".txt" ' delete original
    Open "C:\" & strTable & ".txt.tmp" For Output As #intFileHandle2 ' temp file
    Close #intFileHandle2  ' temp file
    Name "C:\" & strTable & ".txt.tmp" As "C:\" & strTable & ".txt" ' rename temp file
End Sub



ASKER CERTIFIED SOLUTION
Avatar of [ fanpages ]
[ fanpages ]

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
change:

    found = False
   intFileHandle = FreeFile
    Open "C:\" & strTable & ".txt" For Input As #intFileHandle
    intFileHandle2 = FreeFile
    Open "C:\" & strTable & ".txt.tmp" For Output As #intFileHandle2 ' temp file
   
'*** error 'FIle is already open' in next line *******
    Open "C:\" & strTable & ".txt.tmp" For Output As #intFileHandle2 ' temp file

to:

    found = False
   intFileHandle = FreeFile
    Open "C:\" & strTable & ".txt" For Input As #intFileHandle
    intFileHandle2 = FreeFile
    Open "C:\" & strTable & ".txt.tmp" For Output As #intFileHandle2 ' temp file
above post per direction given by  fanpages.
Avatar of ampapa
ampapa

Why not just change the name of your temp file?

Public Sub WriteToTxt(strTable As String, intField As Integer, strFind As String, strReplace As String)
    Dim intFileHandle As Integer
    Dim intFileHandle2 As Integer
    Dim strRETP As String
    Dim FieldArray As Variant
    Dim found As Boolean

    found = False
   intFileHandle = FreeFile
    Open "C:\" & strTable & ".txt" For Input As #intFileHandle
    intFileHandle2 = FreeFile
    Open "C:\" & strTable & ".tmp" For Output As #intFileHandle2 ' temp file
    While Not EOF(intFileHandle)
        Line Input #intFileHandle, strRETP
        If Not found Then
            FieldArray = Split(strRETP, "|")
            If FieldArray(0) = strFind Then
                FieldArray(intField) = strReplace
                strRETP = Join(FieldArray, "|")
                found = True
            End If
        End If
        Print #intFileHandle, strRETP
    Wend
    Close #intFileHandle
    Close #intFileHandle2
    Kill "C:\" & strTable & ".txt" ' delete original
    Open "C:\" & strTable & ".tmp" For Output As #intFileHandle2 ' temp file
    Close #intFileHandle2  ' temp file
    Name "C:\" & strTable & ".tmp" As "C:\" & strTable & ".txt" ' rename temp file
End Sub
Avatar of Bob Lamberson

ASKER

Thanks all three for your quick response and tolerance of my dumb mistake, but mostly for your help.
You're very welcome.

It's amazing how many times a new pair of eyes on the same piece of problematic code can achieve a result very quickly.

Thanks for the points/grading.

Happy codin',

BFN,

fp.
[ http://NigelLee.info ]