Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

Updating a file using Write #

Posted on 2003-03-04
2
Medium Priority
?
181 Views
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)

Thanks!
Matt
0
Comment
Question by:bomax
2 Comments
 
LVL 11

Accepted Solution

by:
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
    Loop
   
    ' 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
    Loop
   
    ' 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

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



0
 

Author Comment

by:bomax
ID: 8068698
Thank you, this is perfect!!
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

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

I’ve seen a number of people looking for examples of how to access web services from VB6.  I’ve been using a test harness I built in VB6 (using many resources I found online) that I use for small projects to work out how to communicate with web serv…
Enums (shorthand for ‘enumerations’) are not often used by programmers but they can be quite valuable when they are.  What are they? An Enum is just a type of variable like a string or an Integer, but in this case one that you create that contains…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
Suggested Courses
Course of the Month12 days, 1 hour left to enroll

564 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