Access to open Access Database and close this database from where I executed the code

pedie

Well-known Member
Joined
Apr 28, 2010
Messages
3,875
Hi is there a way for Access vba to open another Access Database and close this database from where I executed the code?
And if yes how can i make sure that the new database opened in macro enabled?:)

Thanks alot for helping...
Regards
Pedie
 

Excel Facts

Copy a format multiple times
Select a formatted range. Double-click the Format Painter (left side of Home tab). You can paste formatting multiple times. Esc to stop
Yes, you can do this. As for macros enabled you would need to ensure that the database is in a trusted location.

The code to do it would be something like this:
Code:
Function OpenAnotherDb(strFileAndPath As String)
Dim objAcc As Object
 
Set objAcc = CreateObject("Access.Application")
 
objAcc.OpenCurrentDatabase(strFileAndPath)
 
objAcc.Visible = True
objAcc.UserControl = True
Set objAcc = Nothing
 
Application.Quit
End Function

Then you would put that into a standard module and then call it from wherever you needed to like:
Code:
OpenAnotherDb "C:\MyFiles\SomeDatabaseName.accdb"

Does that make sense?
 
Upvote 0
I dont know how to call this function again....
and my database is password protected "TempPwd123"
Thanks again Bob


In standard module
Code:
[/FONT]
[FONT=Courier New]Function OpenAnotherDb(strFileAndPath As String)
Dim objAcc As Object
Dim OpenAnotherDb As String[/FONT]
[FONT=Courier New]OpenAnotherDb = "C:\Users\Pediez\Desktop\Excerise DBs\TEST.accdb"
Set objAcc = CreateObject("Access.Application")
 
objAcc.OpenCurrentDatabase (strFileAndPath)
 
objAcc.Visible = True
objAcc.UserControl = True
Set objAcc = Nothing[/FONT]
[FONT=Courier New]Application.Quit
End Function
Sub testCall()
OpenAnotherDb
End Sub
 
Upvote 0
You lost the file path in your changes. It is like this:

Code:
Function OpenAnotherDb([COLOR="RoyalBlue"]strFileAndPath [/COLOR]As String)
Dim objAcc As Object

Set objAcc = CreateObject("Access.Application")
 
objAcc.OpenCurrentDatabase ([COLOR="RoyalBlue"]strFileAndPath[/COLOR])
 
objAcc.Visible = True
objAcc.UserControl = True
Set objAcc = Nothing
Application.Quit
End Function

Sub testCall()
Dim [COLOR="RoyalBlue"]strFileAndPath[/COLOR]
[COLOR="RoyalBlue"]strFileAndPath [/COLOR]= "C:\Users\Pediez\Desktop\Excerise DBs\TEST.accdb"
Call OpenAnotherDb([COLOR="RoyalBlue"]strFileAndPath[/COLOR])
End Sub
 
Upvote 0
Code:
[FONT=Courier New]Function OpenAnotherDb(strFileAndPath As String)[/FONT]
[FONT=Courier New]Dim objAcc As Object[/FONT]
[FONT=Courier New]Dim OpenAnotherDb As String[/FONT]
[FONT=Courier New][COLOR=red][B]OpenAnotherDb = "C:\Users\Pediez\Desktop\Excerise DBs\TEST.accdb"[/B][/COLOR][/FONT]
[FONT=Courier New]Set objAcc = CreateObject("Access.Application")[/FONT]
 
[FONT=Courier New]objAcc.OpenCurrentDatabase (strFileAndPath)[/FONT]
 
[FONT=Courier New]objAcc.Visible = True[/FONT]
[FONT=Courier New]objAcc.UserControl = True[/FONT]
[FONT=Courier New]Set objAcc = Nothing[/FONT]
[FONT=Courier New]Application.Quit[/FONT]
[FONT=Courier New]End Function[/FONT]
[FONT=Courier New]Sub testCall()[/FONT]
[FONT=Courier New]OpenAnotherDb[/FONT]
[FONT=Courier New]End Sub[/FONT]
For one you don't call this function from inside itself (see red part above and remove it). You would call it from wherever you are wanting to initiate this opening of the database.

As for opening a password protected database you would change the function to this:

Rich (BB code):
Function OpenAnotherDb(strFileAndPath As String, Optional strDbPWD As String)
    Dim objAcc As Object
 
    Set objAcc = CreateObject("Access.Application")
 
    If Len(strDbPWD) > 0 Then
        objAcc.OpenCurrentDatabase strFileAndPath, , strDbPWD
    Else
        objAcc.OpenCurrentDatabase strFileAndPath
    End If
 
    objAcc.Visible = True
    objAcc.UserControl = True
 
    Set objAcc = Nothing
 
    Application.Quit
End Function

And then you would, from the event you want to open the other database from - for example the click event of a button, you would call it:

Rich (BB code):
OpenAnotherDb "C:\Users\Pediez\Desktop\Excerise DBs\TEST.accdb", "TempPwd123"
 
Upvote 0

Forum statistics

Threads
1,224,595
Messages
6,179,799
Members
452,943
Latest member
Newbie4296

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top