HOW TO

Access Related Records for an In-Memory Relate Using ArcObjects

Last Published: April 25, 2020

Summary

Instructions provided describe how to access the related records in an in-memory relate using ArcObjects. An in-memory relate is created by right-clicking a layer in the Table of Contents, then clicking Joins and Relates > Relate.

Procedure



  1. Start 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 then right-click and select View Code.
    [O-Image] Visual Basic Project  Explorer
    Note:
    Code in the Project's ThisDocument code module will only run in the current map document. If you want to store the code in all your map documents open the Normal.mxt ThisDocument code module instead.

  4. Copy the following code into the code module.

    Code:
    Public Sub GetRelatedRecords()

    Dim pDoc As IMxDocument
    Dim pMap As IMap
    Dim pFLayer As IFeatureLayer
    Dim pFClass As IFeatureClass
    Dim pFeat As IFeature
    Dim pRelClasses As IEnumRelationshipClass
    Dim pRelClass As IRelationshipClass
    Dim pOSet As ISet
    Dim pRSet As ISet
    Dim pFCursor As IFeatureCursor
    Dim pRow As IRow
    Dim pDataset As IDataset
    Dim pRCColl As IRelationshipClassCollection

    Set pDoc = ThisDocument
    Set pMap = pDoc.FocusMap
    Set pFLayer = pMap.Layer(0)
    Set pRCColl = pFLayer

    'Build a set of origin features
    Set pFClass = pFLayer.FeatureClass
    Set pOSet = New esriSystem.Set
    Dim pQueryFilter As IQueryFilter
    Set pQueryFilter = New QueryFilter
    pQueryFilter.WhereClause = "code = 'CA10'" '<-- Modify as necessary
    Set pFCursor = pFClass.Search(pQueryFilter, True)
    Set pFeat = pFCursor.NextFeature
    Do Until pFeat Is Nothing
    pOSet.Add pFeat
    Set pFeat = pFCursor.NextFeature
    Loop
    pOSet.Reset
    If pOSet.Count = 0 Then
    MsgBox "Where clause returned no results"
    Exit Sub
    End If

    'Get related objects from memory relationship classes -
    'this loops through all in memory relates
    Set pRelClasses = pRCColl.RelationshipClasses
    pRelClasses.Reset 'get the first relate
    Set pRelClass = pRelClasses.Next
    Do Until pRelClass Is Nothing
    Set pDataset = pRelClass
    Set pRSet = pRelClass.GetObjectsRelatedToObjectSet(pOSet)
    pRSet.Reset
    Set pRow = pRSet.Next
    Do Until pRow Is Nothing
    Debug.Print pDataset.Name & " relationship, Related OID = " & pRow.OID
    Set pRow = pRSet.Next
    Loop
    Set pRelClass = pRelClasses.Next
    Loop

    End Sub

  5. Close the Visual Basic Editor.
  6. Run the code.

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

Article ID:000007600

Software:
  • ArcMap 9 x
  • 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