How To: Apply a selection to a related table


Instructions provided describe how to apply a selection to a related table. In ArcMap, relates can be used to establish a relationship between feature classes and tables. Unlike joins, relates require a command to be issued before applying a tables selection to a related table. This example demonstrates how to programmatically apply a selection from a feature class to a related table in ArcMap using a relationship class.


The sample requires a feature layer in a geodatabase, participating in a relationship class with a standalone table to be loaded into a new ArcMap session. This relationship class must be created in the geodatabase using ArcCatalog; the code does not work properly on relationships created by right-clicking the layer in ArcMap and choosing 'Joins and Relates > Relate...'

  1. Start ArcMap.
  2. Create a new UIButtonControl: How To: Create a new UIButtonControl​
    For more information on creating a UIControl, see the ArcGIS Desktop Help topic: 'Creating custom commands with VBA and UI Controls'
  3. Right-click the UIButtonControl and select View Source.
  4. Copy the following code into the UIButtonControl's click event.
     This code is intended for use in ArcGIS Desktop 9.x. To run this in ArcGIS Desktop 8.x, it is necessary to change one line of code. Please see the comment toward the top of the "GetRelSelection" routine.
    Private Sub GetSelection()
        Dim pDoc As IMxDocument
        Dim pMap As IMap
        Dim pFLayer As IFeatureLayer
        Dim pFClass As IFeatureClass
        Dim pFSelection As IFeatureSelection
        Dim pRelClassColl As IRelationshipClassCollection
        Dim pRelClass As IRelationshipClass
        Dim pEnumRelClass As IEnumRelationshipClass
        Dim pStandAloneTable As IStandaloneTable
        Dim pStandtabcollection As IStandaloneTableCollection
        Dim pTable As ITable
        Dim pInSelSet As ISelectionSet
        Dim pOutSelSet As ISelectionSet
        Dim pEnumRelationshipclass As IEnumRelationshipClass
        Dim pStTab As IStandaloneTable
        Dim pStTabColl As IStandaloneTableCollection
        Dim pRelationshipClass As IRelationshipClass
        Dim pDataset As IDataset
        Dim pOClass As IObjectClass
        Dim pTSelection As ITableSelection
        Dim pTabWin As ITableWindow
        'Get the feature class from the map
        Set pDoc = ThisDocument
        Set pMap = pDoc.FocusMap
        Set pFLayer = pMap.Layer(0)
        Set pFClass = pFLayer.FeatureClass
        'Get the relationship class from the featureclass
        Set pEnumRelationshipclass = pFClass.RelationshipClasses(esriRelRoleAny)
        Set pRelationshipClass = pEnumRelationshipclass.Next
        Set pOClass = pRelationshipClass.DestinationClass
        Set pTable = pOClass
        'Get the selection set
        Set pFSelection = pFLayer
        Set pInSelSet = pFSelection.SelectionSet
        'Get the selection set for the related table
        Set pOutSelSet = GetRelSelection(pInSelSet, pTable, pRelationshipClass)
        'Get related table and add selection set
        Set pStTab = New StandaloneTable
        Set pStTab.Table = pTable
        Set pTSelection = pStTab
        Set pTSelection.SelectionSet = pOutSelSet
        Set pStTabColl = pMap
        pStTabColl.AddStandaloneTable pStTab
        'Create and open a new table window for the table
        Set pTabWin = New TableWindow
        Set pTabWin.Table = pStTab
        Set pTabWin.Application = Application
        pTabWin.UpdateSelection pOutSelSet
        pTabWin.ShowSelected = True
        pTabWin.Show True
    End Sub
    Private Function GetRelSelection(pFrSelSet As ISelectionSet, pToTable As ITable, pRelClass As IRelationshipClass) As ISelectionSet
        Dim pInSet As ISet
        Dim pOutSet As ISet
        Dim pCursor As ICursor
        Dim pRow As IRow
        Dim pOIDList() As Long
        Dim intOIDIndex As Integer
        Dim intCount As Integer
        Dim pOutSelSet As ISelectionSet
        Dim lngOID As Long
        ' Get the set of the selected rows
        ' NOTE: The following line of code is for 9.x installations. To make this work on 8.x,
        ' replace it with this line:
        ' Set pInSet = New esriCore.Set
        Set pInSet = New esriSystem.Set
        pFrSelSet.Search Nothing, False, pCursor
        Set pRow = pCursor.NextRow
        Do While Not pRow Is Nothing
        pInSet.Add pRow
        Set pRow = pCursor.NextRow
        Debug.Print pInSet.Count
        'Get the set of related rows and build an OID list
        Set pOutSet = pRelClass.GetObjectsRelatedToObjectSet(pInSet)
        If pOutSet.Count <> 0 Then
        Set pRow = pOutSet.Next
        ReDim pOIDList(pOutSet.Count - 1)
        intOIDIndex = pRow.Fields.FindField(pToTable.OIDFieldName)
        intCount = 0
        Do While Not pRow Is Nothing
          pOIDList(intCount) = pRow.Value(intOIDIndex)
          Set pRow = pOutSet.Next
          intCount = intCount + 1
        End If
        'Make a selectionset and add the OID's from the OID list - Option #1
        Set pOutSelSet = pToTable.Select(Nothing, esriSelectionTypeHybrid, _
        esriSelectionOptionEmpty, Nothing)
        If pOutSet.Count <> 0 Then
        For lngOID = 0 To pOutSet.Count - 1
          pOutSelSet.Add (pOIDList(lngOID))
        End If
    'Make a selectionset and add the OID's from the OID list - Option #2 (uncomment to use and comment out Option #1)
        'Dim pOutSelSet As ISelectionSet 
        'Set pOutSelSet = pToDispTab.DisplayTable.Select(Nothing, esriSelectionTypeHybrid, esriSelectionOptionEmpty, Nothing) 
        'pOutSelSet.AddList pOutSet.Count, pOIDList(0)     
         Set GetRelSelection = pOutSelSet
    End Function
  5. Make a feature selection from the loaded feature class and click the UIButtonControl.
  6. The selection is applied to the related table as defined by the relationship class. The standalone table is opened, displaying all of the related records.