One To Many Labels
OneToManyParser.cls

' Copyright 2008 ESRI
' 
' All rights reserved under the copyright laws of the United States
' and applicable international laws, treaties, and conventions.
' 
' You may freely redistribute and use this sample code, with or
' without modification, provided you include the original copyright
' notice and use restrictions.
' 
' See use restrictions at <your ArcGIS install location>/developerkit/userestrictions.txt.
' 




Option Explicit

Private m_RelIndex As IRelationshipClass
Private m_FieldIndex As Long
Private m_FieldIndex2 As Long

Implements IAnnotationExpressionParser
Implements IAnnotationExpressionParser2
Implements ICodedValueAttributes

Private Sub Class_Terminate()
  Set m_RelIndex = Nothing
End Sub

Private Property Get IAnnotationExpressionParser_Attribute(ByVal Index As Long) As String
  If Not m_RelIndex Is Nothing Then
    IAnnotationExpressionParser_Attribute = m_RelIndex.OriginPrimaryKey
  Else
    IAnnotationExpressionParser_Attribute = ""
  End If
End Property

Private Property Get IAnnotationExpressionParser_AttributeCount() As Long
  IAnnotationExpressionParser_AttributeCount = 1
End Property

Private Property Get IAnnotationExpressionParser_Expression() As String
  IAnnotationExpressionParser_Expression = ""
End Property

Private Function IAnnotationExpressionParser_FindLabel(ByVal Feature As esriGeoDatabase.IFeature) As String
  IAnnotationExpressionParser_FindLabel = ""
End Function

Private Sub IAnnotationExpressionParser_LastError(number As Long, line As Long, Description As String)
  Description = ""
End Sub

Private Property Get IAnnotationExpressionParser2_Attribute(ByVal Index As Long) As String
  If Not m_RelIndex Is Nothing Then
    IAnnotationExpressionParser2_Attribute = m_RelIndex.OriginPrimaryKey
  Else
    IAnnotationExpressionParser2_Attribute = ""
  End If
End Property

Private Property Get IAnnotationExpressionParser2_AttributeCount() As Long
  IAnnotationExpressionParser2_AttributeCount = 1
End Property

Private Property Get IAnnotationExpressionParser2_Expression() As String
  IAnnotationExpressionParser2_Expression = ""
End Property

Private Function IAnnotationExpressionParser2_FindLabel(ByVal Feature As esriGeoDatabase.IFeature, ByVal pLayerFields As esriCarto.ILayerFields) As String
  On Error GoTo ErrHand:
  If Feature Is Nothing Or pLayerFields Is Nothing Or m_RelIndex Is Nothing Then Exit Function

  Dim pNewSet As esriSystem.ISet, pRow As IRow
  Dim s1 As String, s2 As String, sLabel As String
  Set pNewSet = m_RelIndex.GetObjectsRelatedToObject(Feature)
  pNewSet.Reset
  Set pRow = pNewSet.Next
  sLabel = ""
  Do While Not pRow Is Nothing
    If IsNull(pRow.Value(m_FieldIndex)) Then
      s1 = ""
    Else
      s1 = CStr(pRow.Value(m_FieldIndex))
    End If
    
    If m_FieldIndex2 > -1 Then
      If IsNull(pRow.Value(m_FieldIndex2)) Then
        s1 = s1 & " - "
      Else
        s1 = s1 & " - " & CStr(pRow.Value(m_FieldIndex2))
      End If
    End If
    
    If s1 <> "" Then
      If sLabel = "" Then
        sLabel = s1
      Else
        sLabel = sLabel & vbCrLf & s1
      End If
    End If
    
    Set pRow = pNewSet.Next
  Loop
    
  IAnnotationExpressionParser2_FindLabel = sLabel
  
  Set pRow = Nothing
  Set pNewSet = Nothing

  Exit Function
ErrHand:
  MsgBox "IAnnotationExpressionParser2_FindLabel - " & Erl & " - " & Err.Description
End Function

Private Sub IAnnotationExpressionParser2_LastError(number As Long, line As Long, Description As String)
  Description = ""
End Sub

Private Property Let ICodedValueAttributes_UseCodedValue(ByVal RHS As Boolean)
  
End Property

Private Property Get ICodedValueAttributes_UseCodedValue() As Boolean
  ICodedValueAttributes_UseCodedValue = False
End Property

Public Property Set RelationshipIndex(relIndex As IRelationshipClass)
  Set m_RelIndex = relIndex
End Property

Public Property Let FieldIndex(lIndex As Long)
  m_FieldIndex = lIndex
End Property

Public Property Let FieldIndex2(lIndex As Long)
  m_FieldIndex2 = lIndex
End Property