Link to home
Start Free TrialLog in
Avatar of Mike Eghtebas
Mike EghtebasFlag for United States of America

asked on

error 'FIle is already open' vb6 text file

I am trying to write (replace existing value) in column 3 (0, 1, 2) with string "ABC"
at the record where first field is Smith.

    Call WriteToTxt("TextFileName", 2, "Smith", "ABC")
--------

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
    intFileHandle2 = FreeFile
    Open "C:\" & strTable & ".txt" For Input As #intFileHandle

*** 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 #intFileHandle2, strRETP
    Wend
    Close #intFileHandle
    Close #intFileHandle2
    Kill "C:\" & strTable & ".txt" ' delete original
    Name "C:\" & strTable & ".txt.tmp" As "C:\" & strTable & ".txt" ' rename temp file
End Sub

How can I correct it?

Thanks.
ASKER CERTIFIED SOLUTION
Avatar of Mike Tomlinson
Mike Tomlinson
Flag of United States of America image

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
Open the input file like so
Open "C:\" & strTable & ".txt" For Output Shared As #intFileHandle

Shared allows both input and output.
Hey IM, why the 'SORRY' ?
The sorry is there because the mistake in the code is mine from this PAQ:
https://www.experts-exchange.com/questions/21196806/help-on-write-to-txt-file-vb6.html

I wrote the code right in the browser without using the IDE.  I shoulda tested it first...  

I'm suprised eghtebas accepted it without testing it as well...

=)

~IM
Avatar of Mike Eghtebas

ASKER

Hi Idle_Mind,

I can take you solution to the bank anytime.  They are always good and work fine.  Then again, things like this happens.

Regards,

Mike
eghtebas,

For future reference, if you ever have a problem with code that I provide, please feel free to go back to the original question (even if it is closed) and yell at me.  I will do everything I can to fix it.

I feel badly because this is one of the few times I have supplied code without testing it. =\

~IM
Will do.  Btw, the reason I post a new quewstion because I don't know when you will see my post.  To get an answer asap is the reason, to take advantage of the other experts availibility.

Mike