'VBA- MS ACCESS- Add/Remove library references do not persist after re-opening access file

VBA library references changes do not persist when changed by code? Trying to make code compatible for a number of machines that are a mix of windows 7/10, some that have AutoCAD 2018, and some AutoCAD 2021. my solution is as per below: I have a "startup" form that will add all required references via the below subroutine on startup.

Sub CheckRef(RefName, GUID, Major, Minor)
Dim IsMyReferencePresent As Boolean
Set oRefs = Application.VBE.ActiveVBProject.References
IsMyReferencePresent = False
For Each oRef In oRefs
        If oRef.GUID = GUID Then
            IsMyReferencePresent = True
            Exit Sub
        End If
    Next
    If IsMyReferencePresent = False Then
    On Error Resume Next
        oRefs.AddFromGuid GUID, Major, Minor
    End If

End Sub

I also have a "running" form that is open in the background and on close of DB, it will remove all loaded references using the below code.

 Sub DelRef(GUID)
 Set oRefs = Application.VBE.ActiveVBProject.References
 For Each oRef In oRefs
 If oRef.GUID = GUID Then
 oRefs.Remove oRef
 Exit For
 End If
 Next
 End Sub

I also have a routine to output current library references. as you can see below, just before close , i have successfully removed the references, but on open, they are back still loaded.

Before Access Close

  • Item - Name and Description

  • Item 1 VBAVisual Basic For Applications

  • Item 2 AccessMicrosoft Access 15.0 Object Library

  • Item 3 VBIDEMicrosoft Visual Basic for Applications Extensibility 5.3

  • Item 4 ScriptingMicrosoft Scripting Runtime

On Access Open

  • Item 1 VBA Visual Basic For Applications
  • Item 2 Access Microsoft Access 15.0 Object Library
  • Item 3 VBIDE Microsoft Visual Basic for Applications Extensibility 5.3
  • Item 4 Scripting Microsoft Scripting Runtime
  • Item 5 stdole OLE Automation
  • Item 6 DAO Microsoft Office 15.0 Access database engine Object Library
  • Item 7 Office Microsoft Vbe UI 7.1 Object Library
  • Item 8 ADODB Microsoft ActiveX Data Objects 6.1 Library
  • Item 9 AutoCAD AutoCAD 2021 Type Library

so why are these references persisting even though they are removed?

My first initial thought is that a further recompile is needed before closing access to save the changes. however, doesn't VBA recompile on ref changes? I have tried the below code to recompile but it produces a loading DLL failed error.

 Sub compileMe()
 Dim objVBECommandBar As Object
 Set objVBECommandBar = Application.VBE.CommandBars
 Dim compileMe As CommandBarButton
 Set compileMe = objVBECommandBar.FindControl(Type:=msoControlButton, ID:=578)
 If compileMe.Enabled = True Then compileMe.Execute
 If compileMe.Enabled = True Then Debug.Print "Error Compiling"
 End Sub

Any ideas?



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source