Solved

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

Posted on 2004-09-24
7
364 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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Introduction While answering a recent question about filtering a custom class collection, I realized that this could be accomplished with very little code by using the ScriptControl (SC) library.  This article will introduce you to the SC library a…
When designing a form there are several BorderStyles to choose from, all of which can be classified as either 'Fixed' or 'Sizable' and I'd guess that 'Fixed Single' or one of the other fixed types is the most popular choice. I assume it's the most p…
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…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

708 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

13 Experts available now in Live!

Get 1:1 Help Now