Solved How to check if a database is opened? (1 Viewer)

KitaYama

Well-known member
Local time
Today, 14:09
Joined
Jan 6, 2022
Messages
1,553
I'm looking for a reliable way to check if a given database is opened or not.
For now, I'm checking if the lock file exists or not. But it's not for sure. A database maybe closed and for a lot of reasons, the lock file is not deleted.

Thanks for any kind of advice.
 

Uncle Gizmo

Nifty Access Guy
Staff member
Local time
Today, 06:09
Joined
Jul 9, 2003
Messages
16,287
I wonder, could you try deleting the log file? I'm assuming you won't be able to delete it if the given database is open?

So first step check if the log file exists, if it does, run code to delete it, check again and if it's gone then you know that the database is closed...

I have no idea if this is possible or not, it just seems a route that might work....
 

The_Doc_Man

Immoderate Moderator
Staff member
Local time
Today, 00:09
Joined
Feb 28, 2001
Messages
27,229
You can try for a WinAPI function. This first link tries to open the file exclusively and if it fails, gives you a sharing error. The routine in question then returns either a true or a false Boolean for whether the file is open.


You could also try to directly lock the file exclusively. Again, if it is open, you would get a sharing error because you can't take out an exclusive lock on an already opened file.


A file has a set of usage locks associated with it. These usage locks can be "interest locks" (the mildest lock there is) all the way up to "exclusive write lock" (the strongest lock there is.) If you have the ability to connect to the file's lock information, you could determine how many users have taken out locks. If 0, the file should be free. The problem with this statement is that you have to be privileged in order to get to any information that would identify the other users of the file. (I.e. Run as Admin)

Note that for the two links, (a) either method needs to immediately be reversed if successful (so that YOU don't leave the file locked for others) and (b) if the file is not in use but its lock list has not been correctly reset due to any kind of network glitch or user procedural error, you will get a false positive indication that the file is in use. The only solutions for a false positive are to reboot the system that holds the file OR to wait for a system-wide timeout that causes idle locks to go away OR use admin privileges to try to manually unlock the file. The lock timeout depends on your system admin's choices for the corresponding system setting. The manual unlocking method requires you to be an Admin. The reboot can be done by anyone who has access to the power cord, but doing a brutal power shutdown has its own penalties from users who might come after you with pitchforks and lit torches.
 

KitaYama

Well-known member
Local time
Today, 14:09
Joined
Jan 6, 2022
Messages
1,553
Thanks for the quick response.
Will check both and see how it goes.

Thanks again.
 

arnelgp

..forever waiting... waiting for jellybean!
Local time
Today, 13:09
Joined
May 7, 2009
Messages
19,247
If you can Open the Db in Exclusive Mode then nobody is using the db.
Code:
Public Function IsDbOpen(Byval strDBPath As String) As Boolean
Dim dbs As DAO.Database
If (Dir$(strDBPath)) = 0
    'the db does not exists
    'show message here
    Exit Function
End If
'Opendatabase(strThedbName, bolExclusive, bolReadOnly)
Set dbs = Opendatabase(strDBPath, True, False)
IsDbOpen = (Err = 0)
Err.Clear
Set dbs = Nothing
End Function
 

KitaYama

Well-known member
Local time
Today, 14:09
Joined
Jan 6, 2022
Messages
1,553
@Uncle Gizmo your assumption was correct. Your suggestion worked, but using DELETE, terrifies me (for no reason). Thanks for your solution.

@The_Doc_Man I used the code from your first link. That was perfect. Million thinks.

@arnelgp thanks for taking your time and sharing your solution
1- If (Dir$(strDBPath)) = 0 should be changed to If (Dir$(strDBPath)) = "". I received a type mismatch error.
2- If a the target database is already opened and I run your code, it stops on Set dbs=.... line with a 3706 code error. It doesn't continue running to return True.
3- If the target database is not opened, it always returns true.

I changed your code to the following to be able to use it.
Code:
Public Function IsDbOpen(ByVal strDBPath As String) As Boolean
    Dim dbs As DAO.Database
    On Error GoTo errortrap
    IsDbOpen = True
    If (Dir$(strDBPath)) = "" Then
        'the db does not exists
        'show message here
        Exit Function
    End If
    'Opendatabase(strThedbName, bolExclusive, bolReadOnly)
    Set dbs = OpenDatabase(strDBPath, True, False)
    IsDbOpen = Not (Err = 0)
errortrap:
    Err.Clear
    Set dbs = Nothing
End Function
 

isladogs

MVP / VIP
Local time
Today, 06:09
Joined
Jan 14, 2017
Messages
18,247
Just to add that the presence of a lock file isn't by itself a foolproof test as no lock file is created when a database is opened exclusively.
 

KitaYama

Well-known member
Local time
Today, 14:09
Joined
Jan 6, 2022
Messages
1,553
Just to add that the presence of a lock file isn't by itself a foolproof test as no lock file is created when a database is opened exclusively.
Didn't know that. Thanks for mentioning it.
 

KitaYama

Well-known member
Local time
Today, 14:09
Joined
Jan 6, 2022
Messages
1,553
I just received a message from one of my colleagues that @arnelgp's solution to our question in following post covers this case too.

We had an assumption that Open Filename For Input can only be used for text files, zip or the kind of files that an external file can write to them.
Since we have already that function in our database, we're going to use it for above check too.
@arnelgp Million thanks again

 

Users who are viewing this thread

Top Bottom