write text to file

Sabrin used Ask the Experts™
I would like to have something that can write data to a file,
I have something that writes a name into a text file every 2 seconds
but If I open the same software and click start I will get an error
saying permission denied (run-time error 70) I just want to know
how can I make something to write to the same file without losing
data or with out errors!
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
you should add error handling to your application. You're getting the runtime error because the file is allready in use (most likely) Add an error handler right before you try to open the file that says:

On error goto NoFile

then at the end write.

    End Sub
    msgbox "The file could not be found"
End Sub

This will happen if you have the file open when you're running it, or if you're running the application twice (because then obviously the first instance is using it).

IT is possible to detect multiple instances and prevent them from occuring. It is also possible to open a file as read only and save all the data until the file is available for write permissions.

Let me know what course of action you plan on taking.


the problem here is that the program will be ran with multiple instances..
so it needs to write data to the file! is it possible to do this?
It is actually impossible for two programs to have the same file open in write mode. It's inheritly forbidden by all operating systems.

You have two options:

1. What you need to do is have each instance of the program keep track of what it is doing, either in memory or in a seperate file, and make an attempt to later have only one program open the original file for writing and write all the changes.

2. This option is much more common, but more difficult to implement, you need to alter your resources to share variables. You will have to do this using something called semaphores, which lock the program from performing a cetain task until any other programs trying to use the same resource are finished with this.
a)You could try your luck and have the program open a file, write to it, then close it right away afterwards, but when you do this, you risk the coincidence of two instances trying to have the file open at once, and you will end up with what appears to be "random crashes" when accessing the file.

b)A somewhat safer way to share the resource is to create a for loop, which tests to see if the file is in use, and it keeps on looping until anything else using the file is finished, or you can return a message saying "File is in use, please free up the resource and try to write to file again". Either way, you can open the file only once you are sure no other program is using it.

c)There is a miniscule chance however, that directly after you've decided "yes, this file is free" but directly before you've said "open the file" some other program opens the file, right between those two lines of code. Since that miniscule possibility may occur, especially with many saves happening, you would need to do some research into semaphores in visual basic.

option b can be quite sucessful, and a good temporary solution, but go for c) if you want to guarantee that you will never have a crash when writing to the file.

I hope one of these solutions work out well for you!

Alain Bryden

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial