check if database is already open (1 Viewer)

umair434

Registered User.
Local time
Yesterday, 19:10
Joined
Jul 8, 2011
Messages
186
Hi guys,

I have a form on a database - This form has a button which, when clicked, opens another database for the user. I want the button to set focus to the database if it's already open - Here is the code I have so far:




Private Sub Command1_Click()
Dim appAccess As Access.Application
Dim strDB As String
Dim strUName As String

Const strConPathToSamples = "C:\Documents and Settings\"
strUName = Environ("Username")
Const strFolderC = "\My Documents\Trusted\"
strDB = strConPathToSamples & strUName & strFolderC & "locations.accdb"
Set appAccess = CreateObject("Access.Application")
appAccess.OpenCurrentDatabase strDB, False
Set appAccess = Nothing


End Sub


this code keeps opening the database everytime I click the button. how can I prevent this?

thanks for all the help!
 

joeKra

Registered User.
Local time
Yesterday, 22:10
Joined
Jan 24, 2012
Messages
208
Try to open the database exclusive
 

umair434

Registered User.
Local time
Yesterday, 19:10
Joined
Jul 8, 2011
Messages
186
but I different users should be able to open the database at the same time as well. It is shared over a common drive.
 

brileigh

New member
Local time
Today, 13:10
Joined
Jul 6, 2015
Messages
1
Hi,

I know this is an old thread but someone new might stumble across it:

Try this function:
Private Function isAppDbOpen(oApp As Access.Application) As Boolean
Dim strDummy As String
On Error GoTo isAppDbOpen_Error
strDummy = oApp.CurrentDb.Name
isAppDbOpen = True
isAppDbOpen_Exit:
Exit Function
isAppDbOpen_Error:
isAppDbOpen = False
Resume isAppDbOpen_Exit
End Function

and to test
dim o as Access.Application
debug.print isAppDbOpen(o) '---> False
o_OpenCurrentDatabase "c:\............\Blah.accdb"
debug.print isAppDbOpen(o) '---> True
 

Users who are viewing this thread

Top Bottom