JRB-Bldr,
while it's a good idea to check USED(alias) to prevent ERROR 3 "File is in use", what buddyrich talks about is ERROR 108 "File is in use by another user".
Error 108 cannot be prevented or detected before you use a table, used only checks, if the current process, you yourself already use the table. Error 108 happens, if a table has a header lock by another user, which can happen in by any command writing to a table or by a transaction.
Things to do, to make this error less probable is to make all write operations to tables as short as possible.
Even if you put a USE within a TRY CATCH BLOCK, you can later get an 108 error when writing to that table. I'm not saying TRY CATCH is wrong with this, it's the only valid way to approach error 108 when opening a table, as with exclusive use of a table the only way to detect it is to try to open a table and see if you succeed.
So the same rule also applies to an exclusively open table, which will result in ERROR 1705 "file access is denied".
Error 108 is not handled automatic by USE, it is however handled automatic by INSERT, UPDATE, DELETE, REPLACE, APPEND and such depending on how SET REPROCESS is set. Error 108 can happen with any of these commands too, if Reprocess is set to some timeout or finite number of retries.
The USE of a table is not automatically retried, as it does not lock the table itself, but it is of course affected by a lock.
What you can do when error 108 occurs therefore is setting your app to Sleep for a minimum time of say 0.5 seconds and a random additional time between 0 and 2 seconds. And then do a RETRY. This will take care of deadlock situations, when two clients want the same file. Because both wait a different time, sooner or later you have success. And if the reason is a lengthy update or transaction from another client this is solved too. You may give up retries after this special branch of the errorhandler is hit often in a row.
This is all part of programming for multiple users with shared data access. Unfortunately, when you write to a file, even if you don't lock, VFP has to lock the file at least for the short time, otherwise this would lead to header corruptions or even worse, overwritten or broken records in the table body.
This is the downside of a database, that is no real server, every client has to take the precautions and conflict handling normally only a server has. A server has the advanatage, that it can queue requests on the same data and therefore prevent such problems.
Even shorter said: Read the chapter "Programming For Shared Access" -
And maybe even begin with the Topic "Locking data" (
and read the parts of it explaining automatic locking. There you have a very good overview of what really causes locks and which locks. and get a feeling about what could cause which problems/conflicts and how to handle this.
Bye, Olaf.