VBA List References

I was trying to troubleshoot a new setup with one of my clients over a year ago in which a stable database (8+ years running without any issue) suddenly would not work on the new application server on which I did not have Admin privileges to and was throwing errors which indicate Reference issues.  The problem was that I tried to run the usually suggested code to no avail.

'The following does not work properly and should not be relied upon!
Sub ListReferences()
    On Error Resume Next
    
    Dim ref As Reference
    For Each ref In Application.References
        Err.Clear
        Debug.Print ref.Name, ref.FullPath, ref.GUID, ref.Kind, ref.BuiltIn, ref.IsBroken, ref.Major, ref.Minor
        If Err.Number <> 0 Then
            Debug.Print "Error raised on reference ";
            Debug.Print ref.Name;
            Debug.Print
        End If
    Next ref
End Sub

I turned to asking my fellow MVPs for some guidance and we ended up having quite a substantial discussion and went back and forth with code variations until we perfected the following function.

Sub ListReferences()
'Authors: Dirk Goldgar
'   Contributor: Tom van Stiphout
    On Error Resume Next
    
    Dim ref As Access.Reference
    Dim strRefDescription As String
    Dim lngCount As Long
    Dim lngBrokenCount As Long
    Dim blnBroken As Boolean
    
    Debug.Print "REFERENCES"
    Debug.Print "-------------------------------------------------"
    
    For Each ref In Application.References
        blnBroken = False
        lngCount = lngCount + 1
        strRefDescription = vbNullString
        
        Err.Clear
        strRefDescription = strRefDescription & "Name: '" & ref.Name & "'"
        If Err.Number <> 0 Then
            strRefDescription = strRefDescription & "Name: " & "(error " & Err.Number & ")"
            blnBroken = True
        End If
        
        Err.Clear
        strRefDescription = strRefDescription & ", FullPath: '" & ref.FullPath & "'"
        If Err.Number <> 0 Then
            strRefDescription = strRefDescription & ", FullPath: " & "(error " & Err.Number & ")"
            blnBroken = True
        End If
        
        Err.Clear
        strRefDescription = strRefDescription & ", Guid: " & ref.GUID
        If Err.Number <> 0 Then
            strRefDescription = strRefDescription & ", Guid: " & "(error " & Err.Number & ")"
            blnBroken = True
        End If
        
        Err.Clear
        strRefDescription = strRefDescription & ", Kind: '" & ref.Kind & "'"
        If Err.Number <> 0 Then
            strRefDescription = strRefDescription & ", Kind: " & "(error " & Err.Number & ")"
            blnBroken = True
        End If
 
        Err.Clear
        strRefDescription = strRefDescription & ", BuiltIn: " & ref.BuiltIn
        If Err.Number <> 0 Then
            strRefDescription = strRefDescription & ", BuiltIn: " & "(error " & Err.Number & ")"
            blnBroken = True
        End If
        
        Err.Clear
        strRefDescription = strRefDescription & ", IsBroken: " & ref.IsBroken
        If Err.Number <> 0 Then
            strRefDescription = strRefDescription & ", IsBroken: " & "(error " & Err.Number & ")"
            blnBroken = True
        End If
        
        Err.Clear
        strRefDescription = strRefDescription & ", Major: " & ref.Major
        If Err.Number <> 0 Then
            strRefDescription = strRefDescription & ", Major: " & "(error " & Err.Number & ")"
            blnBroken = True
        End If
        
        Err.Clear
        strRefDescription = strRefDescription & ", Minor: " & ref.Minor
        If Err.Number <> 0 Then
            strRefDescription = strRefDescription & ", Minor: " & "(error " & Err.Number & ")"
            blnBroken = True
        End If
        
        If blnBroken Then
            lngBrokenCount = lngBrokenCount + 1
            strRefDescription = "*BROKEN* " & strRefDescription
        End If
        
        Debug.Print strRefDescription
        
    Next ref
    
    Debug.Print "-------------------------------------------------"
    Debug.Print lngCount & " references found, " & lngBrokenCount & " broken."

    If lngBrokenCount <> 0 Then
        MsgBox "Broken References were found in the VBA Project!", vbCritical + vbOKOnly
    End If
End Sub

Armed with this new procedure I was quickly able to identify the source of my problems, the server admins had mixed office application versions so my early binding libraries (Yes, this db was created long before I discovered the benefits of Late Binding!!!) were MISSING because I was told they were running Office 2010, when in fact they were running Access 2010 with Excel and Word 2007! Oops, I guess it slipped their minds! Then I switched everything over to Late Binding. One more reason to always used Late Binding!!!

2 responses on “VBA List References

  1. John Reilly

    This is a really excellent way of debugging and the code is well laid out.
    However, there is a small error in the published code.. “blnBroken” should be set to false around the line “strRefDescription = vbNullString” otherwise once the first error happens the flag wll always be set for all subsequent loop through the references, causing the “*BROKEN* ” to appear at the start of all subsequent lines.

    1. Daniel Pineault Post author

      What you say make sense at first glance. I will take a closer look a little later.