Solved How to close a database?

KitaYama

Well-known member
Local time
Today, 16:07
Joined
Jan 6, 2022
Messages
1,540
I'm trying to close another database if it's opened.
it seems there's a method for this purpose, but Microsoft doesn't offer any example of the usage.

This is what I have so far:

Code:
    Dim Pth As String
    Pth = CurrentProject.path & "\AnotherDatabase.laccdb"
    If fFileExists(Pth) Then
        ....Do some Actions here
        'Close it
        DoCmd.CloseDatabase CurrentProject.path & "\AnotherDatabase.accdb"
        Exit Function
    Else
        Application.Quit
    End If

I receive a compile error on DoCmd : Wrong Number of Arguments or invalid property assignment

My questions:
1- While in DatabaseA , How can I close DatabaseB? What is the best method to check if a database is opened and close it?
2- What's the correct usage of above method?

I appreciate any kind of help.
 
Last edited:

Eugene-LS

Registered User.
Local time
Today, 10:07
Joined
Dec 7, 2018
Messages
481
How can I close DatabaseB?
Code:
    Dim OtherDB As Object
    Dim Pth As String
    Pth = CurrentProject.Path & "\AnotherDatabase.laccdb"
    If Not Dir(Pth) = "" Then
        '....Do some Actions here
        'Close it
        Pth = CurrentProject.Path & "\AnotherDatabase.accdb"
        Set OtherDB = GetObject(Pth)
        OtherDB.Application.Quit
        Exit Function
    Else
        Application.Quit
    End If
    Set OtherDB = Nothing
 

sonic8

AWF VIP
Local time
Today, 09:07
Joined
Oct 27, 2015
Messages
998
My questions:
1- While in DatabaseA , How can I close DatabaseB? What is the best method to check if a database is opened and close it?
2- What's the correct usage of above method?
Re 1:
Code:
Dim accApp As Access.Application
Set accApp = GetObject("c:\path\to\other\db.accdb")
If not accApp is nothing then
   accApp.Quit
Endif
(Just typed here and not tested.)

Re 2:
It helps to read the documentation you linked to.
DoCmd.CloseDatabase closes the current database and it does not expect any argument!
 

KitaYama

Well-known member
Local time
Today, 16:07
Joined
Jan 6, 2022
Messages
1,540
While the suggested methods works, it's very strange that I have to set an object as Access database while I'm in Access to work on it.
I hoped I may find something like how Excel closes opened workbooks :

Code:
Workbooks("BOOK1.XLS").Close

But if that's how Access works, nothing else can be said.

Million thanks for your help.
 

arnelgp

..forever waiting... waiting for jellybean!
Local time
Today, 15:07
Joined
May 7, 2009
Messages
19,230
tell us How did you open the db.
did you use OpenDatabase method
of Access.Application?

it depends. you can Forcefully close it using API.
 

arnelgp

..forever waiting... waiting for jellybean!
Local time
Today, 15:07
Joined
May 7, 2009
Messages
19,230
Set accApp = GetObject("c:\path\to\other\db.accdb")
using the above method, If db.accdb is Not Already Open, It will get Open.
therefore defeat your purpose.

for all you know .laccdb is not Guarantee that the db is open.
it can happen that a power interruption occurs therefore left
the .laccdb without deleting it.
 

KitaYama

Well-known member
Local time
Today, 16:07
Joined
Jan 6, 2022
Messages
1,540
for all you know .laccdb is not Guarantee that the db is open.
it can happen that a power interruption occurs therefore left
the .laccdb without deleting it.
Is there any other way to be certain if a database is opened?
 

arnelgp

..forever waiting... waiting for jellybean!
Local time
Today, 15:07
Joined
May 7, 2009
Messages
19,230
you Open it Exclusively.
if it is already opened, either Shared or Exclusive, you will get Error.
Code:
Function IsDatabaseRunning(strDBName As String) As Boolean
'   Function to check if a database is already running
'   Accepts:
'       The path and name of an Access database
'   Returns:
'       True if the database can't be opened (because it is already open)
'       False if the database can be opened (because it is not already open)
    On Error GoTo E_Handle
    IsDatabaseRunning = True
    Dim db As Database
    Set db = DBEngine(0).OpenDatabase(strDBName, True)
    IsDatabaseRunning = False
fExit:
    On Error Resume Next
    db.Close
    Set db = Nothing
    Exit Function
E_Handle:
    Select Case Err.Number
        Case 3704        ' Database already opened
        Case 3045        ' Database already open as well
        Case Else
            MsgBox "E_Handle  " & Err.Description, vbOKOnly + vbCritical, "Error: " & Err.Number
    End Select
    Resume fExit
End Function
 

KitaYama

Well-known member
Local time
Today, 16:07
Joined
Jan 6, 2022
Messages
1,540
you Open it Exclusively.
if it is already opened, either Shared or Exclusive, you will get Error.
Code:
Function IsDatabaseRunning(strDBName As String) As Boolean
'   Function to check if a database is already running
'   Accepts:
'       The path and name of an Access database
'   Returns:
'       True if the database can't be opened (because it is already open)
'       False if the database can be opened (because it is not already open)
    On Error GoTo E_Handle
    IsDatabaseRunning = True
    Dim db As Database
    Set db = DBEngine(0).OpenDatabase(strDBName, True)
    IsDatabaseRunning = False
fExit:
    On Error Resume Next
    db.Close
    Set db = Nothing
    Exit Function
E_Handle:
    Select Case Err.Number
        Case 3704        ' Database already opened
        Case 3045        ' Database already open as well
        Case Else
            MsgBox "E_Handle  " & Err.Description, vbOKOnly + vbCritical, "Error: " & Err.Number
    End Select
    Resume fExit
End Function
Million Thanx
 

KitaYama

Well-known member
Local time
Today, 16:07
Joined
Jan 6, 2022
Messages
1,540
Just as a follow up for others who may have the same problem.

I followed @Eugene-LS suggestion first using this line of code:
Code:
OtherDB.Application.Quit
Some PCs were able to close the database instantly, a lot of them took more than a minute or two to close the database.
It seemed like Access is in a loop trying to close the database. During this one or two minutes, Access window is frozen and it can not even be terminated using task manager.

I later changed to @sonic8 version :
Code:
OtherDB.Quit
This version had the same effect, but almost all PCs showed the same result. Only one PC was able to close the database instantly, others took 2 to 3 minutes.

A research on google showed that the correct version is : (or at least correct version for me)
Code:
OtherDB.Docmd.Quit
This one was able to close the database instantly on all PCs.

All tests were done on Windows 10, Microsoft 365 (32,64 bit) both with latest updates.


Again thanks for all your help.
 
Last edited:

Users who are viewing this thread

Top Bottom