HOW TO

Programmatically create and populate an area field in ArcMap

Last Published: April 25, 2020

Summary

Note:
This article pertains to ArcGIS versions 8.x. Later versions of ArcGIS may contain different functionality, as well as different names and locations for menus, commands and geoprocessing tools.

This article contains sample code that adds a field called Area to your polygon attribute table and populates it with the area for each polygon.

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

Procedure

  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 pDoc As IMxDocument
  Dim pMap As IMap
  
  Set pDoc = ThisDocument
  Set pMap = pDoc.FocusMap
  
  '-- Get the selected layer
  Dim pFLayer As IFeatureLayer
  Set pFLayer = pDoc.SelectedLayer
  
  Dim pFc As IFeatureClass
  Set pFc = pFLayer.FeatureClass
  '-- Check to see if layer already has an area field
  If Not pFc.FindField("Area") = -1 Then
     MsgBox "Area already exists"
     Exit Sub
  End If
  '-- Create a field called Area
  Dim pField As IField
  Set pField = New Field
  Dim pFieldEdit As IFieldEdit
  Set pFieldEdit = pField
  With pFieldEdit
    .Name = "Area"
    .Type = esriFieldTypeDouble
    .Precision = 11
    .Length = 12
    .Scale = 3
  End With
  pFc.AddField pField

 '-- Loop through the features and get the area
  Dim pFCursor As IFeatureCursor
  Set pFCursor = pFLayer.Search(Nothing, False)
  Dim pFeat As IFeature
  Set pFeat = pFCursor.NextFeature

  Do Until pFeat Is Nothing
    Dim pArea As IArea
     If pFeat.Shape.GeometryType = esriGeometryPolygon Then
       Set pArea = pFeat.Shape
       pFeat.Value(pFc.FindField("Area")) = pArea.Area
       pFeat.Store
     End If
    Set pFeat = pFCursor.NextFeature
  Loop
  1. Select the layer in the Table of Contents and run the code.
Note:
The selected layer should not have an existing field called area

Article ID:000004030

Software:
  • ArcMap 8 x

Get help from ArcGIS experts

Contact technical support

Download the Esri Support App

Go to download options

Discover more on this topic