The following code samples demonstrate how to get related objects from a table based on the selected records in the first table. Sample one demonstrates doing this from destination table to origin table and sample two demonstrates going from origin table to destination table.
Sample One: Destination to Origin
Private
Sub GetSelection_fromRelated()
Dim pDoc As IMxDocument
Dim pMap As IMap
Dim pFLayer As IFeatureLayer
Dim pFClass As IFeatureClass
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 pQf As IQueryFilter
Dim pInSelSet As ISelectionSet
Dim pOutSelSet As ISelectionSet
'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 relate from the feature layer
Set pRelClassColl = pFLayer
Set pEnumRelClass = pRelClassColl.RelationshipClasses
Set pRelClass = pEnumRelClass.Next
'Make sure relate was found uses first relate on first layer in the map
If pRelClass Is Nothing Then
MsgBox "No relate on this featureclass"
Exit Sub
End If
'Get the standalone table from the map
Set pStandtabcollection = pMap
Set pStandAloneTable = New StandaloneTable
Set pStandAloneTable = pStandtabcollection.StandaloneTable(0)
Set pTable = pStandAloneTable.Table
'Create a scratch workspace factory to use for the selection
Dim pScratchWorkspace As IWorkspace
Dim pScratchWorkspaceFactory As IScratchWorkspaceFactory
Set pScratchWorkspaceFactory = New ScratchWorkspaceFactory
Set pScratchWorkspace = pScratchWorkspaceFactory.DefaultScratchWorkspace
'Create a query filter
Set pQf = New QueryFilter
pQf.WhereClause = "STATE_NAME = 'Oregon'"
'Get the selection set
'Set pInSelSet = pFClass.Select(pQf, esriSelectionTypeHybrid,
esriSelectionOptionNormal, pScratchWorkspace)
Set pInSelSet = pTable.Select(pQf, esriSelectionTypeHybrid,
esriSelectionOptionNormal, Nothing)
Debug.Print "Destination Class selection count is: " &
pInSelSet.Count
'Get the selection set from the related table
Set pOutSelSet = GetRelSelection(pInSelSet, pFClass, pRelClass)
Debug.Print "Origin Class selection count is: " & pOutSelSet.Count
End Sub
Private Function
GetRelSelection(pFrSelSet As ISelectionSet, pToFClass As IFeatureClass, _
pRelClass As IRelationshipClass) As ISelectionSet
Dim pInSet As esriSystem.ISet
Dim pOutSet As esriSystem.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
Set pInSet = New esriSystem.Set
pFrSelSet.Search Nothing, False, pCursor
Set pRow = pCursor.NextRow
Do While Not pRow Is Nothing
pInSet.Add pRow
'Debug.Print pRow.value(53)
Set pRow = pCursor.NextRow
Loop
pInSet.Reset
Debug.Print "Destination Class is: " &
pRelClass.DestinationClass.AliasName
Debug.Print "Origin Class is: " & pRelClass.OriginClass.AliasName
'Debug.Print "pInSet Count is: " & pInSet.Count
'Get the set of related rows and build an OID list
Set pOutSet = pRelClass.GetObjectsRelatedToObjectSet(pInSet) 'Object Required
Error here
If pOutSet.Count <> 0 Then
Set pRow = pOutSet.Next
ReDim pOIDList(pOutSet.Count - 1)
intOIDIndex = pRow.Fields.FindField(pToFClass.OIDFieldName)
intCount = 0
Do While Not pRow Is Nothing
pOIDList(intCount) = pRow.value(intOIDIndex)
Set pRow = pOutSet.Next
intCount = intCount + 1
Loop
End If
'Make a selectionset and add the OID's from the OID list
Set pOutSelSet = pToFClass.Select(Nothing, esriSelectionTypeHybrid, _
esriSelectionOptionEmpty, Nothing)
If pOutSet.Count <> 0 Then
For lngOID = 0 To pOutSet.Count - 1
pOutSelSet.Add (pOIDList(lngOID))
Next
End If
Set GetRelSelection = pOutSelSet
End Function
Sample Two: Origin to Destination
Private Sub GetSelection()
Dim pDoc As IMxDocument
Dim pMap As IMap
Dim pFLayer As IFeatureLayer
Dim pFClass As IFeatureClass
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 pQf As IQueryFilter
Dim pInSelSet As ISelectionSet
Dim pOutSelSet As ISelectionSet
'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 relate from the feature layer
Set pRelClassColl = pFLayer
Set pEnumRelClass = pRelClassColl.RelationshipClasses
Set pRelClass = pEnumRelClass.Next
'Make sure relate was found uses first relate on first layer in the map
If pRelClass Is Nothing Then
MsgBox "No relate on this featureclass"
Exit Sub
End If
'Get the standalone table form the map
Set pStandtabcollection = pMap
Set pStandAloneTable = New StandaloneTable
Set pStandAloneTable = pStandtabcollection.StandaloneTable(0)
Set pTable = pStandAloneTable.Table
'Create a query filter
Set pQf = New QueryFilter
pQf.WhereClause = "STATE_NAME = 'Oregon'"
'Get the selection set
Set pInSelSet = pFClass.Select(pQf, esriSelectionTypeHybrid,
esriSelectionOptionNormal, Nothing)
'Get the selection set from the related table
Set pOutSelSet = GetRelSelection(pInSelSet, pTable, pRelClass)
Debug.Print pOutSelSet.Count
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
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
Loop
pInSet.Reset
'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
Loop
End If
'Make a selectionset and add the OID's from the OID list
Set pOutSelSet = pToTable.Select(Nothing, esriSelectionTypeHybrid, _
esriSelectionOptionEmpty, Nothing)
If pOutSet.Count <> 0 Then
For lngOID = 0 To pOutSet.Count - 1
pOutSelSet.Add (pOIDList(lngOID))
Next
End If
Set GetRelSelection = pOutSelSet
End Function