Check if db is open (1 Viewer)

JohnPapa

Registered User.
Local time
Today, 14:39
Joined
Aug 15, 2010
Messages
954
I would like to be able to check if a db is open and if it is to prevent the user from proceeding.

I use the Access 2013 Runtime. I can check if a db is open by trying to open Exclusively, using something like

Code:
https://support.microsoft.com/en-gb/kb/210359

But if the db is already open Not exclusively and I try to open it exclusively it does not raise an error and it allows the user to open the db in Read Only mode.

I would like to be able to prevent this, because careless and/or inexperienced users may open several instances of the db.

Of course, if the db is already open exclusively it does not allow you to open it exclusively another time.

Thanks
 

spikepl

Eledittingent Beliped
Local time
Today, 13:39
Joined
Nov 3, 2010
Messages
6,142
I used the code below picked up somewhere to prevent more than one instance of my current db to open. It can probably be adopted to your needs.

Updtae: On reflection, is there no way to see how a particualr insatnce has been opened ? Id' look into that. Because the solution I gave you can be noticeably slow.

Code:
Function IsRunning() As Integer
    Dim DB As dao.Database


    If gcfHandleErrors Then On Error GoTo IsRunning_Error

    Set DB = CurrentDb

    If TestDDELink(DB.Name) Then

        '("PTD Shipping is already open")

        DB.Close

        Set DB = Nothing

        DoCmd.Quit

    End If


IsRunning_Exit:

    On Error Resume Next

    Exit Function

IsRunning_Error:

    Select Case Err.Number

        Case Else

            MsgBox "Error " & Err.Number & " (" & Err.Description & ") in procedure IsRunning of Module InitTools"

            Resume IsRunning_Exit

    End Select

End Function



Function TestDDELink(ByVal strAppName$) As Integer

    Dim varDDEChannel

    On Error Resume Next

    Application.SetOption ("Ignore DDE Requests"), True

    varDDEChannel = DDEInitiate("MSAccess", strAppName)

    ' When the app isn't already running this will error

    If Err Then

        TestDDELink = False

    Else

        TestDDELink = True

        DDETerminate varDDEChannel

        DDETerminateAll

    End If

    Application.SetOption ("Ignore DDE Requests"), False

End Function
 

JohnPapa

Registered User.
Local time
Today, 14:39
Joined
Aug 15, 2010
Messages
954
Thanks spikepl, will try it out.

A sort of relevant question, if I open exclusively the FE on pc1 can I open the same FE exclusively on pc2? Also can I change the BE db on pc3 from pc1 and pc2 when open exclusively?

Thanks
 

spikepl

Eledittingent Beliped
Local time
Today, 13:39
Joined
Nov 3, 2010
Messages
6,142
It doesn't check for exclusive or not. It checks for instances either on same machine or under same login name - I suspect the former. So it is also not related to your question about BE.
 

JohnPapa

Registered User.
Local time
Today, 14:39
Joined
Aug 15, 2010
Messages
954
Unfortunately, the same login may appear on other pcs.
 

vbaInet

AWF VIP
Local time
Today, 12:39
Joined
Jan 22, 2010
Messages
26,374
There's an API that stops multiple instances of an application from being instantiated but I can't remember what it's called.

However, if we think about this in simple terms, GetObject() will fail if the db is opened so you could give that a try. The other thing to do is to check for a lock file, but the only downside with this is that if the user's db crashes the lock file doesn't get deleted. For the third option, there's a schema in Access that lists users.
 

ryetee

Registered User.
Local time
Today, 12:39
Joined
Jul 30, 2013
Messages
952
There's an API that stops multiple instances of an application from being instantiated but I can't remember what it's called.

However, if we think about this in simple terms, GetObject() will fail if the db is opened so you could give that a try. The other thing to do is to check for a lock file, but the only downside with this is that if the user's db crashes the lock file doesn't get deleted. For the third option, there's a schema in Access that lists users.
is the API something like createmutex?

Oh and don't know if this works but there's code here..
http://access.mvps.org/access/api/api0041.htm
 
Last edited:

Users who are viewing this thread

Top Bottom