English

How To: Update Attributes window when selecting features programmatically

Summary

Note: 
Support for Visual Basic for Applications (VBA) for ArcMap and ArcCatalog ended with the ArcGIS 10.2.2 release, and Esri has not included VBA compatibility setups since version 10.5. See: ArcGIS Desktop and VBA Moving Forward

The Attributes Inspector window updates automatically when a user selects a feature with a tool on the map. However, this may not be true when a feature is selected programmatically.

Procedure

The following sample illustrates how to select a feature programmatically and to update the Attributes Inspector. The key is to use ISelectionEvents to notify the Attributes Inspector.

  1. Start ArcMap.
  2. Create a new UIButtonControl: How To: Create a new UIButtonControl
Note:
For more information on creating a UIControl, see the ArcGIS Desktop Help topic 'Creating custom commands with VBA and UI Controls'
  1. Right-click the UIButtonControl and select View Source.
  2. Copy this code into the UIButtonControl's click event.
Dim pMxDoc As IMxDocument
Dim pMap As IMap
Dim pFSel As IFeatureSelection
    
' Build a query to select the first feature
Dim pQF As IQueryFilter
Set pQF = New QueryFilter
pQF.WhereClause = "Fid = 1"
    

Dim pFeatCur As IFeatureCursor
    
' Return a reference to the map
Set pMxDoc = ThisDocument
Set pMap = pMxDoc.FocusMap

' Clear the prior selection
pMap.ClearSelection
    
' Select a feature
Set pFSel = pMap.Layer(0)
pFSel.SelectFeatures pQF, esriSelectionResultNew, False
    
'Refresh the Active View
Dim pAV As IActiveView
Set pAV = pMap
pAV.PartialRefresh 2, pFSel, Nothing
' **************************************
  1. Click AddData to load one editable layer.
  2. From the Tools Menu, click on the Editor toolbar.
  3. Click Editor > Start Editing.
  4. On the Editor toolbar, click the Attributes button to open the Attributes Inspector.
  5. Click on the new button to run the code.
  6. Notice the Attributes Inspector is empty; this occurs even though a feature has been selected because ISelectionEvents::SelectionChanged was not called.
  7. Copy this code into the UIButtonControl's click event, just before End Sub.
Dim pSelEvents As ISelectionEvents
Set pSelEvents = pMap
pSelEvents.SelectionChanged
  1. Click the button to run the code. The code has programmatically selected a feature and updated the Attributes Inspector.