Mike Eghtebas
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.
at the record where first field is Smith.
Call WriteToTxt("TextFileName",
--------
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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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
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
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
I can take you solution to the bank anytime. They are always good and work fine. Then again, things like this happens.
Regards,
Mike
I am getting an error in the same code, same line. Please see
https://www.experts-exchange.com/questions/21197779/file-is-already-open-vb-6-text-file.html
https://www.experts-exchange.com/questions/21197779/file-is-already-open-vb-6-text-file.html
ASKER
a follow-up question at:
https://www.experts-exchange.com/questions/21197789/text-file-and-memo-field-vb6.html
Thanks,
Mike
https://www.experts-exchange.com/questions/21197789/text-file-and-memo-field-vb6.html
Thanks,
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
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
ASKER
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
Mike
Open "C:\" & strTable & ".txt" For Output Shared As #intFileHandle
Shared allows both input and output.