Solved

Opening a textfile and update it without overwriting existing content..

Posted on 2004-09-24
7
371 Views
Last Modified: 2010-05-02
Folks
I hope this is a nice easy straight forward VB6 question for someone

I want to open a file and save data to it, without overwriting the data that is there. I also want to set it up in such away that numerous users can run my VB app and all will be able to update the same text file simultaneously.
ie. 20 users = 20 updates to the same textfile ?

Possible ??
Sample cod ewould be great :>)

Thanks
Paul
0
Comment
Question by:skintpaul
7 Comments
 
LVL 8

Accepted Solution

by:
mladenovicz earned 250 total points
ID: 12143322
Open pathname For mode [Access access] [lock] As [#]filenumber [Len=reclength]

access mode should be Append, Lock can be Lock Write
0
 
LVL 8

Expert Comment

by:mladenovicz
ID: 12143348
sorry, mode should be Append

Open FileName For Append LockWrite As #1

Here is info about lock param
Keyword specifying the operations restricted on the open file by other processes: Shared, Lock Read, Lock Write, and Lock Read Write.
0
 
LVL 8

Expert Comment

by:mladenovicz
ID: 12143381
Ooops,

Open FileName For Append Lock Write As #1
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 26

Expert Comment

by:EDDYKT
ID: 12143399
May be this

Private Sub INSERTLOG(ByVal msg As String)
    Dim FileNo As Integer
   
    On Error Resume Next
    FileNo = FreeFile
    Open "c:\temp\t.txt" For Append Access Write Lock Write As #FileNo
    Print #FileNo, msg
    Close #FileNo
End Sub

I highly recommend to create a separate task to accept the data and write to file
0
 
LVL 16

Expert Comment

by:jimbobmcgee
ID: 12143541
If you just want to allow 20 people to open a file at once, you could use

     Open "myfile.txt" for Output Shared as #1
or  Open "myfile.txt" for Append Shared as #1

but I have no idea what will happen when -- I imagine the last person to Close #1 that file will have his changes committed.

Does the text entered have any relevance on the text wntered before it (e.g. a log file just reports whats hapening at a certain time and sticks it on the end; it doesn't care about anything that happened before -- same with some kind of chat program).  

If so, you may wish to consider some kind of 'commit' system, to only open the file and append when a line is complete, but you would have no control over where the line was added.

If any part of that text file could be edited at any time, then you are going to need to use some temporary files and then a decision making process to determine whose edit gets priority.  Either that, or allowing each user to only edit one line at a time (locking that line for all others).  Then a server-side app could send locked/unlocked flags to the other users, which would block access or perform a refresh.

Do the other users need to see what is happening to the text file at the same time?  

If so, your clients are going to have to keep polling the file to see if it has changed, then reloading the file if it has.  This might be resource-intensive, though.

Programs like Word or Excel will not allow you to open a file that is already open, by design, except in a Read-Only or copied state.  It isn't easy to implement a system like this.

What do you need the functionality for?  Maybe there is a better way...?
0
 

Author Comment

by:skintpaul
ID: 12143609
Thanks for the quick response. I'm about to add another VB question.
Cheers
Skinpaul
0
 
LVL 16

Expert Comment

by:jimbobmcgee
ID: 12143685
You could use a combination of shared, lock read and lock write, along with a timer control.  Use the timer control to determine when to read the data from the file, changing the countdown length randomly if it cannot access the file.  This would reduce the risk of two users, stuck in a sychronous update schedule, repeatedly being unable to commit changes...
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
You can of course define an array to hold data that is of a particular type like an array of Strings to hold customer names or an array of Doubles to hold customer sales, but what do you do if you want to coordinate that data? This article describes…
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…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

756 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