Fritz Paul
asked on
How to know whether database is already opened by another user
I use Access 2010.
I would like some code to run every time the back end database is opened by the first user, but not if it is opened by subsequent users.
To explain another way: user A opens the front end database, say numbers.accdb and the back end database numbers_be.accdb is still closed, then the code runs (which will automatically imply that the back_end is activated). However if user B then opens the front end, the code does not run.
How can I determine whether a user is the first user to open the back end database?
I would like some code to run every time the back end database is opened by the first user, but not if it is opened by subsequent users.
To explain another way: user A opens the front end database, say numbers.accdb and the back end database numbers_be.accdb is still closed, then the code runs (which will automatically imply that the back_end is activated). However if user B then opens the front end, the code does not run.
How can I determine whether a user is the first user to open the back end database?
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
But do you want this to run ANYTIME no one has the database open, and a user opens it?
Or do you want this to run at certain intervals (like daily, or weekly)?
If the second, then Dale's code would take care of that.
If the first then ... I'm not sure how you'd do this. You could check for the lockfile, but of course as soon as you open the database Access will create the lockfile, so you'd always return a positive. You could use the JetRoster to determine who has the file open. Here's the FMS article that explains how to do that:
http://www.fmsinc.com/MicrosoftAccess/monitor/database-users.htm
Essentially, you'd implement that code, and check to see if ONLY the current machine name is in that list. If so, then you can assume you're the "first" to open the database, and fire your code. If not, then you can assume someone else has already opened the database, and you don't need to fire the code.
However, if you could tell us more about exactly what needs to be done once-and-only-once, we might be able to come up with better suggestions.
Or do you want this to run at certain intervals (like daily, or weekly)?
If the second, then Dale's code would take care of that.
If the first then ... I'm not sure how you'd do this. You could check for the lockfile, but of course as soon as you open the database Access will create the lockfile, so you'd always return a positive. You could use the JetRoster to determine who has the file open. Here's the FMS article that explains how to do that:
http://www.fmsinc.com/MicrosoftAccess/monitor/database-users.htm
Essentially, you'd implement that code, and check to see if ONLY the current machine name is in that list. If so, then you can assume you're the "first" to open the database, and fire your code. If not, then you can assume someone else has already opened the database, and you don't need to fire the code.
However, if you could tell us more about exactly what needs to be done once-and-only-once, we might be able to come up with better suggestions.
ASKER
Guys you are great!
Jim's answer is spot on, but the other two answers are so valuable.
Jim's answer is spot on, but the other two answers are so valuable.
When you open the front-end, run this subroutine which would check to see whether the date stored in that field is todays date. If not, then immediately write todays date to that field and then run your process. It might look something like:
Open in new window