Solved

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

Posted on 2004-09-24
7
366 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

When trying to find the cause of a problem in VBA or VB6 it's often valuable to know what procedures were executed prior to the error. You can use the Call Stack for that but it is often inadequate because it may show procedures you aren't intereste…
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.
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…
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…

910 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now