Solved

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

Posted on 2004-09-24
7
367 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
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 
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

ScreenConnect 6.0 Free Trial

Explore all the enhancements in one game-changing release, ScreenConnect 6.0, based on partner feedback. New features include a redesigned UI, app configurations and chat acknowledgement to improve customer engagement!

Question has a verified solution.

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

Introduction In a recent article (http://www.experts-exchange.com/A_7811-A-Better-Concatenate-Function.html) for the Excel community, I showed an improved version of the Excel Concatenate() function.  While writing that article I realized that no o…
Background What I'm presenting in this article is the result of 2 conditions in my work area: We have a SQL Server production environment but no development or test environment; andWe have an MS Access front end using tables in SQL Server but we a…
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…
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…

777 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