Reputation: 3680
Is it possible to close one Access database (let's call it Database A) via VBA code from another Database (Database B).
In the example here, if Database A is open when Database B launches, I would want Database A to close. Is this possible using VBA?
I've had a google about, but all answers seem to be related to closing the current database using VBA, which of course I could achieve with DoCmd.Quit.
Any help is greatly appreciated.
Upvotes: 2
Views: 16345
Reputation: 1
Application.CurrentDb.Close
Application.FollowHyperlink (CurrentProject.Path & "\MyNewDB.accdb")
DoCmd.Quit acQuitPrompt 'i dont know why its closed my previous Database
Thanks. Forgive me if i am wrong.
Upvotes: 0
Reputation: 31
I know this question is old, but maybe this will help someone. I had a database in which I used VBA to open another db and run a macro. I wanted to then close the db. The code which worked for me is below, it includes the entire process from open to close.
Function functionName()
Static acc As Access.Application
Dim db As DAO.Database
Dim dbname As String
dbname = "absolutePathToDBHere.accdb"
Set acc = New Access.Application
acc.Visible = True
Set db = acc.DBEngine.OpenDatabase(dbname, False, False)
acc.OpenCurrentDatabase dbname
acc.DoCmd.RunMacro ("macroName")
acc.DoCmd.Quit acQuitSaveAll
db.Close
Set db = Nothing
End Function
Upvotes: 3
Reputation: 91326
You can hijack it:
Dim OtherDB As Object
sOther = "Z:\Documents\other.accdb"
Set OtherDB = GetObject(sOther)
OtherDB.Application.Quit
It may make life difficult for someone.
Upvotes: 7