HOW TO
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.
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.
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
Get help from ArcGIS experts
Download the Esri Support App