Access VBA - relative file references

2019-02-17 08:25发布

问题:

In have built a couple of mda library files which I am then referencing from my main Access application (i.e. using Tools -> References from within the IDE).

Is there a way that these references can be made relative rather than absolute. The reason I am asking is so that it would make it easy to set-up on the user's computer if all three files (main application and two mda files) could simply be placed in any directory and work without having to change the references...

Thanks

回答1:

Why not just place the three MDEs in the same folder on the target system? Access should find the MDE references just fine.

Or are you using the add-in logic with the USysRegInfo table? You don't really need to do that with your own add-ins. Just with developer type add-ins such as Rick Fisher's Find and Replace.

If this isn't working for you then tell us what error messages or symptoms.



回答2:

To get the file path for the access application

CurrentProject.Path & "\"

Then just add the other files into the same directory and get them by name. i.e.

Dim filepath As String
filepath = CurrentProject.Path & "\name_of_file.mda"


回答3:

You can add VBA references through VBA itself.

Dim sFilename As String
sFilename = CurrentProject.Path & "\" & whatever.mda

Application.References.AddFromFile sFilename

Just put that in your AutoExec and that reference ought to be available for everything. Of course, you'll have to check if the reference already exists before adding it, otherwise you get an error. But that's just a matter of looping through Application.References.