Given a database object in MS Access VBA, how can one get that database's VBProject?
Function GetVBProject(ByVal db As Database) As VBProject
Set GetVBProject = ???
End Function
The only way I know how to get VBProjects in Access is through Application.VBE.VBProjects.Item(???). However, I won't know what order of the projects are in and what the name is. I will only know it's parent database. The equivalent in Excel would be simply
Function GetVBProject(ByVal wb As Workbook) As VBProject
Set GetVBProject = wb.VBProject
End Function
Look in the
VBProjectscollection and check each project'sFileNameproperty. If a project'sFileNameis the current database file (CurrentDb.Name), that is the one you want.That function returns the project name. You could use the name to set a reference to the
VBProjectobject. Or you could revise the function to return theVBProjectinstead of a string.I barely tested this, so I'm uncertain
objVBProject.FileName = CurrentDb.Namewill be the correct test condition for every situation. But I hope this answer gives you something useful to build on.I looked into
objVBProject.FileNamevs.CurrentDb.Namewhen the db is opened from a drive letter and from a UNC path to a network share. Either way, it seemsobjVBProject.FileNameandCurrentDb.Nameboth "self-adjust" and still match each other: