How to check if any user is logged in before backup ?

I am using a backup utility where in at the time of exiting the application, user is asked just to ensure that every user is logged out and than the db is copied to local computer of the user. Its only a message box, doing nothing except warning.. :)

Can you suggest just to check if any user is there already logged in so that backup process can be halted to avoid damaging db ?
Will be grateful for the answer.

Kamlesh JainManagerAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Dale FyeOwner, Developing Solutions LLCCommented:
Well, if you don't have any bound forms open, and don't have any global recordset or database objects open, then the BE file will not have a .laccdb file.  Generally, I do my backups from my splash screen, which loads before any bound forms or any other objects are created.  It is also the last form to close in my applications, so all other forms are closed at that point.  If the .laccdb file for the BE file still exists, then you know there is still someone else in the database.

Also check out this reference which uses the UserRoster
Scott McDaniel (Microsoft Access MVP - EE MVE )Infotrakker SoftwareCommented:
You can check for the existence of a lockfile before you backup. The lockfile is named <YourDatabaseName>.laccdb. If that file exists, then someone has it open.

Better still - use a real backup utility, like Windows Backup. You don't really have to worry about such things, since the backup system will take care of those.

As with any backup, you should periodically check the backups to be sure they are what you expect. Nothing worse than recovering from a disaster only to find the your backups are bad as well ...
Jim Dettman (Microsoft MVP/ EE MVE)President / OwnerCommented:
I would not use the ldb file as an indicator as it can be incorrect in some cases.   99% of the time it is accurate, but if someone Ctrl/Alt/Deletes out, or doesn't have delete priv for the directory, then it will remain.

 What you can do is attempt to open the DB exclusively.   If you can't do that, then someone else is in the DB.

 But as Scott said, you'd be better off with backup software that has an open file option and uses VSS to snapshot the file.  That might not give you a consistent DB though from a user viewpoint.

Protecting & Securing Your Critical Data

Considering 93 percent of companies file for bankruptcy within 12 months of a disaster that blocked access to their data for 10 days or more, planning for the worst is just smart business. Learn how Acronis Backup integrates security at every stage

Jim Dettman (Microsoft MVP/ EE MVE)President / OwnerCommented:
Function CanOpenDbExclusively(strDBPath As String) As Boolean

        Dim dbe As PrivDBEngine
        Dim wrk As Workspace
        Dim dbs As Database

        Const conFileInUse = 3045
        Const conDBOpenedExclusively = 3356

10      On Error Resume Next

20      Set dbe = New PrivDBEngine
30      Set wrk = dbe.Workspaces(0)
40      Set dbs = wrk.OpenDatabase(strDBPath, True)

50      If dbs Is Nothing Then
          ' If error is unanticipated, display message.
60        If (Err = conFileInUse Or Err = conDBOpenedExclusively) Then
            ' If database cannot be opened exclusively, return False.
70          CanOpenDbExclusively = False
80        Else
90          MsgBox "Error: " & Err & ": " & vbCrLf & Err.Description
100       End If
110     Else
          ' If database can be opened exclusively, return True.
120       CanOpenDbExclusively = True
130       dbs.Close
140     End If

End Function

Open in new window


Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Kamlesh JainManagerAuthor Commented:
Dear Mr Jim,
Would be obliged if you could brief what exactly this function does ?
Will it allow copying (Backup) even if some one is already using this database ?
Or it will warn the user that some one is using the db ?
Kamlesh Jain
Jim Dettman (Microsoft MVP/ EE MVE)President / OwnerCommented:

  It lets you know if anyone else has the DB file open for any purpose.

  The function attempts to open the DB exclusively...if it can do that, then no one else is in the database.  If it cannot do that, then someone is still in the database.  So for a complete solution, you would:

1. Lock users out of the database using your own built-in method, or use the JET passive shutdown feature.   Using your own built-in method is generally better as it gives you more control.

2.  Ensure that everyone is out of the DB using the function I posted.

3. "backup" the DB by copying the file or using backup software.

4. Allow users back in to the DB.

 Doing the above steps gives you a copy of the database that is in a consistent state (not in the middle of an operation).

 If you just copy a DB file while it is in use, or even use backup software with an open file option, you may not get that.    For example, someone is in the middle of a month end close process and they have completed a couple of steps and are just finishing up.  You will get a backup, but it will be while they were in the middle of the process.  Typically however if do either of those in off-hours, your pretty safe.

 Following the steps above however is a a sure thing and you know you have a solid backup.

Scott McDaniel (Microsoft Access MVP - EE MVE )Infotrakker SoftwareCommented:
Jim's solution seems to be the most complete and robust
Kamlesh JainManagerAuthor Commented:
Dear Experts.
I am extremely sorry for not responding on time as i was on prolonged holidays. To be honest, i have not yet tested the above.
Shall revert asap.

Thanking you,
Kamlesh Jain
Jim Dettman (Microsoft MVP/ EE MVE)President / OwnerCommented:

  Not a problem.   Hope you enjoyed your time off.

  If you do have any problems in getting it working, just post back here again and we will get you going.

It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft Access

From novice to tech pro — start learning today.