How to get around open files

I'm writing a simple chat-type program that works by reading a file on the network.  

All is fine except for sending a message when other users are "listening" or reading the files.  There is a delay in the listening and obviously "time holes" where data could be written.

However, I'm having difficulty getting this to work.

I've got properly written ErrorHandlers (they work the first time, then the second time they get through to the 'open "file.dat" for output access write as #1' statement they give a Run-time error 70 -- "Permission Denied")  

Am I missing something here?  I've tried Timers, and loops to "check" but the permission denied error still persists (even though there's an errorhandler!)

Does anyone have any ideas why?  and if so, some code to show me =P

Thanks a bunch,
Who is Participating?
nrgiserConnect With a Mentor Commented:
Check your options. You have 3 options for errorhandling. When it says 'Always Break on Errors', it does what it says. It always breaks when an error occurs. Even when you have errorhandlers
Only one error handler can be active at a time.  Make sure that once you have entered the error handler GoTo, you either call Resume Next, or Exit Sub.  If you encounter another error while still in the error handler, it will not be handled.  In other words, you cannot have nested error handlers.

Post a snippet of your error code.

This set of lines will keep trying until it's possible to open the file.

on error resume next
  open "c:\chat.txt" for output as 1
loop while err
on error goto 0

Ture Magnusson
Karlstad, Sweden
If you want to replace the file chat with a really small winsock chat program let me know, and I'll post the code....

this code could help:

dim file_no as integer
file_no = Freefile

open "file.dat" for output access write as #file_no

Actually what happens is that the OS allots a file no. to each open file. Now when the first file is being opened, it would get file no 1. The next time you say open #1, it tries to open an open file and fails.
Freefile gets around this problem by querying the OS for the next free file no.
But be careful, this could open too many instances of the same file if many clients are connected.

Good luck.
All Courses

From novice to tech pro — start learning today.