How to remove all joins from a layer or table in ArcMap


This sample removes the all joined data from the table or layer selected in the table of contents. This sample performs the same function as choosing the Remove All Joins command from the layer or table context menu.
Once the sample is executed, you can click the Joins and Relates tab in layer or table properties to see that all joins have been removed. If the layer or table's table window was opened when you ran the sample, you must close and re-open the table window in order to remove the joined columns.

How to use

  1. Paste the code into VBA.
  2. In the table of contents, select the layer or table to remove joins from. Thesample will warn you if the layer or table has no joins.
  3. Execute the routine.
[VBA]
Public Sub RemoveAllJoins()
    
    On Error GoTo EH
    
    Dim pDoc As IMxDocument
    Set pDoc = ThisDocument
    Dim pMap As IMap
    Set pMap = pDoc.FocusMap
    
    ' Get the selected layer or table
    Dim pSelItem As IUnknown
    Set pSelItem = pDoc.SelectedItem
    If pSelItem Is Nothing Then
        MsgBox "Nothing is selected in the table of contents"
        Exit Sub
    End If
    If Not (TypeOf pSelItem Is ILayer Or TypeOf pSelItem Is IStandaloneTable) Then
        MsgBox "A layer or table must be selected."
        Exit Sub
    End If
    
    Dim pDispRC As IDisplayRelationshipClass
    Set pDispRC = pSelItem
    
    ' Remove all joins
    If pDispRC.RelationshipClass Is Nothing Then
        MsgBox "The layer or table is not joined."
        Exit Sub
    End If
    
    pDispRC.DisplayRelationshipClass Nothing, esriLeftInnerJoin
    
    Exit Sub
EH:
    
    MsgBox Err.Number & "  " & Err.Description
    
End Sub