Add references programmatically

后端 未结 4 1958
长情又很酷
长情又很酷 2020-12-09 19:45

we have an Access-Application which does not work on some clients, mainly because references are broken. That happens for example when you start the access application with

4条回答
  •  悲&欢浪女
    2020-12-09 20:06

    Here is a code sample, which checks for broken references. I know this is not the whole solution for you, but it will give you some clues how to do it.

    Public Function CheckRefs()
        On Error GoTo Handler
    
        Dim rs As Recordset
        Dim ref As Reference
        Dim msg As String
    
        For Each ref In Application.References
            ' Check IsBroken property.
            If ref.IsBroken = True Then
                msg = msg & "Name: " & ref.Name & vbTab
                msg = msg & "FullPath: " & ref.FullPath & vbTab
                msg = msg & "Version: " & ref.Major & "." & ref.Minor & vbCrLf
            End If
        Next ref
    
        If Len(msg) > 0 Then MsgBox msg
        Exit Function
    
    Handler:
        ' error codes 3075 and 3085 need special handling
    
        If Err.Number = 3075 Or Err.Number = 3085 Then
            Err.Clear
            FixUpRefs
        Else
            rs.Close
            Set rs = Nothing
        End If
    End Function
    
    Private Sub FixUpRefs()
        Dim r As Reference, r1 As Reference
        Dim s As String
    
        ' search the first ref which isn't Access or VBA
        For Each r In Application.References
            If r.Name <> "Access" And r.Name <> "VBA" Then
                Set r1 = r
                Exit For
            End If
        Next
        s = r1.FullPath
    
        ' remove the reference and add it again from file
        References.Remove r1
        References.AddFromFile s
    
        ' hidden syscmd to compile the db
        Call SysCmd(504, 16483)
    End Sub
    

提交回复
热议问题