Set references of a library from a sub in that library

R

Riccardo

I want to set some references of a library i use in a main project. The code
to set them is written inside that library, here it is:

-------------------------------
Private Sub Aggiorna_Percorso_Libreria(ByVal nome As String, ByVal percorso
As String)
On Error GoTo Errore
Dim i As Byte
For i = 1 To CodeProject.Application.References.Count
If CodeProject.Application.References(i).Name = nome Then
If CodeProject.Application.References(i).FullPath = percorso
Then Exit Sub
CodeProject.Application.References.Remove
CodeProject.Application.References(i)
Exit For
End If
If CodeProject.Application.References(i).Name = percorso Then
CodeProject.Application.References.Remove
CodeProject.Application.References(i)
End If
Next i
If VBA.Dir(percorso) = "" Then MsgBox "Libreria mancante al percorso: "
& percorso & VBA.Chr(10) & "L'applicazione verra' chiusa", vbCritical,
"Errore critico": DoCmd.Quit ' Notare il VBA.Chr(10) e Dir. Sfruttato nel
caso una libreria non sia collegata
CodeProject.Application.References.AddFromFile percorso
Exit Sub
Errore:
MsgBox "Errore durante la registrazione della libreria: " & percorso,
vbCritical, "Errore critico"
DoCmd.Quit
End Sub
---------------------

This code is tested and works properly if i run it directly from the
library. The problem is that if i call that sub from the program that uses
the library, the references are set in the program that called that sub, not
in the library.

Example:

References before sub call:

Main.accdb
- Vba
- dao

Library.accdb
- Vba
- dao

References after sub call:

Main.accdb
- vba
- dao
- new reference

Library.accdb
- vba
- dao

But the new reference should be in the Library.accdb reference list not in
the main.
I know it's a bit complicated but i hope you can help me anyway.

Thanks
 
M

Marshall Barton

Riccardo said:
I want to set some references of a library i use in a main project. The code
to set them is written inside that library, here it is:

-------------------------------
Private Sub Aggiorna_Percorso_Libreria(ByVal nome As String, ByVal percorso
As String)
On Error GoTo Errore
Dim i As Byte
For i = 1 To CodeProject.Application.References.Count
If CodeProject.Application.References(i).Name = nome Then
If CodeProject.Application.References(i).FullPath = percorso
Then Exit Sub
CodeProject.Application.References.Remove
CodeProject.Application.References(i)
Exit For
End If
If CodeProject.Application.References(i).Name = percorso Then
CodeProject.Application.References.Remove
CodeProject.Application.References(i)
End If
Next i
If VBA.Dir(percorso) = "" Then MsgBox "Libreria mancante al percorso: "
& percorso & VBA.Chr(10) & "L'applicazione verra' chiusa", vbCritical,
"Errore critico": DoCmd.Quit ' Notare il VBA.Chr(10) e Dir. Sfruttato nel
caso una libreria non sia collegata
CodeProject.Application.References.AddFromFile percorso
Exit Sub
Errore:
MsgBox "Errore durante la registrazione della libreria: " & percorso,
vbCritical, "Errore critico"
DoCmd.Quit
End Sub
---------------------

This code is tested and works properly if i run it directly from the
library. The problem is that if i call that sub from the program that uses
the library, the references are set in the program that called that sub, not
in the library.


I have not tried that kind of thing, but I think you need to
use CodeDb instead of CodeProject.

Also, you should set an object variable at the top of the
code instead of referring to CodeDb throughout the
procedure.
 
R

Riccardo

Codedb refers to the database in which the code is currently running and it's
a Database type. So i can't set references from that.
 
R

Riccardo

Simply from what i've seen i need a method to reach my library references
instead of my main application references. Because both Codeproject and
Currentproject access to my main.accdb references.
 
M

Marshall Barton

Riccardo said:
Simply from what i've seen i need a method to reach my library references
instead of my main application references. Because both Codeproject and
Currentproject access to my main.accdb references.


Ahh right, the Application object. Hmmm, skating on thin
ice, I seem to remember something about changing a reference
also requiring a (re)compile and that a library must be
compiled as its own application. If my memory is
approximately correct, then changing a reference is strictly
a design time operation. At runtime, your best bet is to
use late binding instead of messing with references.
 
T

Tony Toews [MVP]

Marshall Barton said:
Hmmm, skating on thin
ice, I seem to remember something about changing a reference
also requiring a (re)compile and that a library must be
compiled as its own application. If my memory is
approximately correct, then changing a reference is strictly
a design time operation.

Correct. You can't change references in an MDE.
At runtime, your best bet is to
use late binding instead of messing with references.

Not sure how you can do that and use the various function calls into
the referenced MDB/MDE. Hmm, maybe that would work. I'd have to
experiment with all that a bit.

Tony
--
Tony Toews, Microsoft Access MVP
Please respond only in the newsgroups so that others can
read the entire thread of messages.
Microsoft Access Links, Hints, Tips & Accounting Systems at
http://www.granite.ab.ca/accsmstr.htm
Tony's Microsoft Access Blog - http://msmvps.com/blogs/access/
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top