Sinister Beard
Sinister Beard

Reputation: 3680

Closing a database using VBA code from a seperate database

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

Answers (3)

Zia Hassan
Zia Hassan

Reputation: 1

Hello Sir Im noob but it works on me.

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

kristine
kristine

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

Fionnuala
Fionnuala

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

Related Questions