English

How To: Select features based on joined fields

Summary

This article shows how to use VBA and ArcObjects to select features based on attribute values in a joined table.

Procedure

These samples assume you have created a join between a feature layer and a table through the ArcMap GUI.

  • This code sample selects features based on the joined fields.

    Code:
    Sub SelectFeatures()
    Dim pMxDoc As IMxDocument
    Set pMxDoc = ThisDocument
    Dim pFeatureLayer As IFeatureLayer
    Set pFeatureLayer = pMxDoc.FocusMap.Layer(0)
    ' The Display Table has the joined fields.
    Dim pDisplayTable As IDisplayTable
    Set pDisplayTable = pFeatureLayer
    ' Note the syntax for a joined field is table_name.field_name.
    Dim pQueryFilter As IQueryFilter
    Set pQueryFilter = New QueryFilter
    pQueryFilter.WhereClause = "tract_pop.POPULATION > 4000"
    ' Make the selection in the display table.
    Dim pSelSet As ISelectionSet
    Set pSelSet = pDisplayTable.SelectDisplayTable(pQueryFilter, _
    esriSelectionTypeHybrid,esriSelectionOptionNormal, Nothing)
    ' Apply the selection to the feature class.
    Dim pFeatSel As IFeatureSelection
    Set pFeatSel = pFeatureLayer
    Set pFeatSel.SelectionSet = pSelSet
    ' Refresh the display.
    Dim pActiveView As IActiveView
    Set pActiveView = pMxDoc.FocusMap
    pActiveView.Refresh
    MsgBox pSelSet.count & " features are now selected."
    End Sub

  • This code sample returns the result as a feature cursor.

    Code:
    Sub CursorThruFeatures()
    Dim pMxDoc As IMxDocument
    Set pMxDoc = ThisDocument
    Dim pFeatureLayer As IFeatureLayer
    Set pFeatureLayer = pMxDoc.FocusMap.Layer(0)
    ' The Display Table has the joined fields.
    Dim pDisplayTable As IDisplayTable
    Set pDisplayTable = pFeatureLayer
    ' Note the syntax for a joined field is table_name.field_name.
    Dim pQueryFilter As IQueryFilter
    Set pQueryFilter = New QueryFilter
    pQueryFilter.WhereClause= _
    "tract_pop.POPULATION > 4000"
    ' Make the cursor from the display table.
    Dim pFeatCursor As IFeatureCursor
    Set pFeatCursor = pDisplayTable.SearchDisplayTable _
    (pQueryFilter, False)
    ' Loop thru the cursor and count the features
    Dim pFeat As IFeature
    Set pFeat = pFeatCursor.NextFeature
    Dim count As Long
    Do Until pFeat Is Nothing
    count = count + 1
    Set pFeat = pFeatCursor.NextFeature
    Loop
    MsgBox "The cursor has " & count & " features."
    End Sub

    You can loop through the resulting features but the selection does not display in ArcMap.