Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

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

Posted on 2004-09-24
7
Medium Priority
?
379 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
[X]
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
7 Comments
 
LVL 8

Accepted Solution

by:
mladenovicz earned 1000 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
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.

 
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

Introduction While answering a recent question (http://www.experts-exchange.com/Q_27402310.html) in the VB classic zone, I wrote some VB code in the (Office) VBA environment, rather than fire up my older PC.  I didn't post completely correct code o…
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…
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 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

715 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