Referencing VBA Project in Access: Is that possible like in excel?

pedie

Well-known Member
Joined
Apr 28, 2010
Messages
3,875
Hi,:)
Is it possible to run a macro from a different database, than the one currently using?
Many Thanks

Pedie


 

Excel Facts

Test for Multiple Conditions in IF?
Use AND(test, test, test, test) or OR(test, test, test, ...) as the logical_test argument of IF.
the database has to be opened to access the other module, a simple way could be to make a macro and name it (exactly) AUTOEXEC this will fire when ever the database is opened... so you could simply trigger an event to open the database.

With Code to run a specific function... this will do the trick....


Code:
Private Sub Command4_Click()

'full path to the database including name....
Const strDBpath As String = "C:\Users\ziggy\Documents\Database1.mdb"

Dim objAccDB As Object

    Set objAccDB = GetObject(strDBpath) 'opens other the other database

    objAccDB.Run ("testFunct") 'runs a function in the other database
    objAccDB.Quit 'closes the other database after running code

    Set objAccDB = Nothing


End Sub
 
Upvote 0
Hi Ziggy, thanks that was great! Thank you soo much.
Can you also tell me how to do that if the file is password protected? I mean the 2nd db where the macro i want to run from is if protected...


Thanks again:)
 
Upvote 0
Hey pedie,

Trying to make me think eh... :)

here check this out, I used a different method as I don't think I can pass a "password" with the getobject ( at least I don't know how).

this is working for me.....


Code:
Public Function OpenDatabasePass()

Static AccApp As Access.Application
Const strDBpath As String = "C:\Users\ziggy\Documents\Database1.mdb"
Set AccApp = New Access.Application
AccApp.Visible = True

Dim db As DAO.Database

Set db = AccApp.DBEngine.OpenDatabase(strDBpath, False, False, ";PWD=ziggy")

AccApp.OpenCurrentDatabase strDBpath

' comment out or remove thse lines if you don't want it to close automatically...
'AccApp.CloseCurrentDatabase
AccApp.Quit
db.Close
Set db = Nothing


End Function
 
Upvote 0
Pedie

Why would you want to run code from a different database?
 
Upvote 0
In excel this use to be very usefull thing to me and my team. I use one file to store codes so that I can change the code without having my users to change thier copy which is front end to access main database this way i can take control of few things to some extent..


Thanks again for very usefull information.

Regards
Pedie
 
Upvote 0
You can reference another vb project the same way as in Excel.
In the VBE:
Tools | References
Then browse out to the other database and add it.
Also, be sure the project has a unique name (you must change this under Properties to something other than the default - use a project name that starts with a letter or underscore and has no spaces or unusual characters in it. As long as the two database have different project names the external DB can be referenced).

This is a good way to store your shared or general code library. By the way, you can also create Access addins - but in that case each person needs to install it if you intend to distribute it, so it's not what you want here.
 
Last edited:
Upvote 0

Forum statistics

Threads
1,224,587
Messages
6,179,734
Members
452,939
Latest member
WCrawford

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