Vb.net – How to Add/Remove reference programmatically

ms-accessvb.net

My Application is built to a scan MS Access database in VB.NET.

When the Access application is distributed to end users, they may have different versions of COM components. Is it possible to Add/Remove references programmatically to resolve broken references due to differing versions?

Please share me code or link for reference.

Best Answer

Here is some sample code:

Create Reference from File

  Sub AddWS()
  'Create a reference to Windows Script Host, '
  'where you will find FileSystemObject ' 
  'Reference name: "IWshRuntimeLibrary" '
  'Reference Name in references list: "Windows Script Host Object Model" '
  ReferenceFromFile "C:\WINDOWS\System32\wshom.ocx"
  End Sub

  Function ReferenceFromFile(strFileName As String) As Boolean
  Dim ref As Reference

         On Error GoTo Error_ReferenceFromFile
         References.AddFromFile (strFileName)
         ReferenceFromFile = True

  Exit_ReferenceFromFile:
         Exit Function

   Error_ReferenceFromFile:
         ReferenceFromFile = False
         Resume Exit_ReferenceFromFile
   End Function 

Delete Reference

  Sub DeleteRef(RefName)
  Dim ref As Reference

      'You need a reference to remove '
      Set ref = References(RefName)
      References.Remove ref

  End Sub 


You can use the references collection to find if a reference exists.

Reference Exists

  Function RefExists(RefName)
  Dim ref As Object

     RefExists = False

     For Each ref In References
         If ref.Name = RefName Then
             RefExists = True
         End If
     Next

  End Function

From: http://wiki.lessthandot.com/index.php/Add,_Remove,_Check_References

You may also wish to read http://www.mvps.org/access/modules/mdl0022.htm