File Manipulation

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

How to Read and Write to the same file?

I have a text file want to search for a particular line and then replace that line with a new line for the same text file. How can you do that in Access VBA?

Thanks
Dilani
 
Hi Dilani,

Assuming it's a delimited file, you can't edit it "in place". Instead,
create a new file and copy the old one line by line until you come to
the line in question; then write the new line; and finally write the
remainder of the file.

This untested air code should get you started:

Dim lngFIn As Long, lngFOut As Long
Dim strLine As String
Dim strFName As String
Dim strTempName As String
Dim strFindLine As String
Dim strReplLine As String

strFName = "D:\Folder\File.txt"
strTempName = "D:Folder\File.$$$"

strFindLine = "Find Me"
strReplLine = "New Line"


lngFIn = FreeFile()
lngFOut = FreeFile()
Open strFName For Input As #lngFIn
Open strTempName For Output As #lngFOut

Do
Line Input #lngFIn, strLine
If strLine = strFindLine Then
Print #lngFOut, strReplLine
Else
Print #lngFOut, strLine
End If
Loop Until EOF(#lngFIn)

Close #lngFIn
Close #lngFOut
Kill strFName
Name strTempName As StrFName
 
Back
Top