List relates in ArcMap using ArcObjects

Last Published: April 25, 2020


This article contains sample code that lists the names of all relates for the first layer displayed in the table of contents in ArcMap.


  1. Open ArcMap.
  2. Open the Visual Basic Editor.

    In ArcMap, select Tools > Macros > Visual Basic Editor.

  3. In the Project Explorer window, expand Project.mxt and select ArcMap Objects > ThisDocument. Right-click and select View Code.
    [O-Image] Visual Basic Project  Explorer
    Code in ThisDocument code module will only run in the current map document. To store the code in all map documents, open the code in the Normal.mxt ThisDocument code module.

  4. Paste the following code into the code module.

    Sub GetLayer()
    Dim pDoc As IMxDocument
    Set pDoc = ThisDocument
    Dim pMap As IMap
    Set pMap = pDoc.FocusMap

    Dim pLayer As IFeatureLayer
    Set pLayer = pMap.Layer(0)
    ListRelates pLayer
    End Sub

    Sub ListRelates(pFLayer As IFeatureLayer)
    Dim pRelClassColl As IRelationshipClassCollection
    Set pRelClassColl = pFLayer

    Dim pEnumRelClass As IEnumRelationshipClass
    Dim pRelClass As IRelationshipClass
    Set pEnumRelClass = pRelClassColl.RelationshipClasses
    Dim pDs As IDataset
    Set pRelClass = pEnumRelClass.Next

    Do Until pRelClass Is Nothing
    'List the name of the relate
    Set pDs = pRelClass
    MsgBox pDs.Name
    Set pRelClass = pEnumRelClass.Next
    End Sub

  5. Close the Visual Basic Editor.
  6. Add data to ArcMap and establish some relates for the first layer in the table of contents.
  7. Run the GetLayer subroutine.

    A. Click Tools > Macros > Macros to display the Macros dialog box.
    B. Select a macro and click Run.

Article ID:000004973

  • ArcMap 8 x

Get help from ArcGIS experts

Contact technical support

Download the Esri Support App

Go to download options

Discover more on this topic