Updating a file using Write #

Posted on 2003-03-04
Medium Priority
Last Modified: 2010-05-01
Hi, I have a file open for sequential access and I want to update the third line but leave all other lines intact.  How can I do it?

I would have written the file using random access (put#)but I figured that would be a bad idea because I don't know the length of any of my fields.  (I only know if they are strings or ints or bools)

Question by:bomax
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
LVL 11

Accepted Solution

supunr earned 200 total points
ID: 8068243
What you might have to do here is create a temporary file, which is a copy of the file and change this file.  Refer to the code below.

Private Sub UpdateLine()
    On Error GoTo UpdateErr
    Dim strFile As String
    Dim strTmpFile As String
    Dim strLine As String
    strFile = "C:\FileName.txt"
    strTmpFile = "C:\FileName.tmp"

    If (Dir(strTmpFile) <> "") Then ' tmp file exist
        Kill strTmpFile ' delete the file
    End If
    Open strFile For Input As #1
    Open strTmpFile For Output As #2
    ' copy until the line you want
    Do While (Not EOF(1))
        Input #1, strLine
        If (strLine = "<Whatever you want>") Then
            Exit Do
        End If
        Write #2, strLine
    ' write the changed line
    Write #2, "<Changed Line>"
    ' now write the rest of the file
    Do While (Not EOF(1))
        Input #1, strLine
        Write #2, strLine
    ' close open files
    Close #2
    Close #1
    If (Dir(strTmpFile) <> "") Then ' tmp file exist
        Kill strFile ' delete the original file
        ' rename the tmp file as original file
        Name strTmpFile As strFile
    End If

    Exit Sub

    MsgBox "Error updating the file." & vbCrLf & Err.Number & ": " & Err.Description
    Reset ' close any open files
    On Error GoTo 0
End Sub


Author Comment

ID: 8068698
Thank you, this is perfect!!

Featured Post

New benefit for Premium Members - Upgrade now!

Ready to get started with anonymous questions today? It's easy! Learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

There are many ways to remove duplicate entries in an SQL or Access database. Most make you temporarily insert an ID field, make a temp table and copy data back and forth, and/or are slow. Here is an easy way in VB6 using ADO to remove duplicate row…
If you have ever used Microsoft Word then you know that it has a good spell checker and it may have occurred to you that the ability to check spelling might be a nice piece of functionality to add to certain applications of yours. Well the code that…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
Suggested Courses
Course of the Month10 days, 18 hours left to enroll

770 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question