I'm having an issue where when I remove a reference from Access on close it does not stick. I have the function assigned to a hidden window that is always open and it works when I manually close the window, but when I close the database the change to remove the reference doesn't stick. We are having issues since part of our team is on Office 2013 so we have had to make a dynamic reference point in the VBA Code. The code to add the reference works fine, but removing it is the issue.
Here's the code for adding it Public Function RunThis()
Dim ref As Reference
'For Each ref In Access.References
'MsgBox ref.Name
'Next
If Dir("C:\Program Files (x86)\Microsoft Office\Office15\EXCEL.exe") <> "" Then
Access.References.AddFromFile ("C:\Program Files (x86)\Microsoft Office\Office15\EXCEL.exe")
End If
If Dir("C:\Program Files (x86)\Microsoft Office\Office16\EXCEL.exe") <> "" Then
Access.References.AddFromFile ("C:\Program Files (x86)\Microsoft Office\Office16\EXCEL.exe")
End If
End Function
Here's the code that is run on exit
Private Sub Form_Close()
Dim ref As Reference
For Each ref In Access.References
If ref.Name = "Excel" Then
Access.Application.References.Remove ref
'MsgBox "Found It"
End If
MsgBox ref.Name
Next
DoCmd.Save
End Sub