Create Summary Table

Created:12/11/2000
Description:

A table containing summary statistics for a layer is created with this sample. The output is a standalone table which is added to ArcMap. The sample can be modified to create a new summary layer where shapes of the same class are dissolved. This sample mimics the behavior of the summarize command in the column context menu of the table window.

How to use:
  1. Paste the code into VBA.
  2. Add a layer named US_Counties to ArcMap or adjust the layer name. This layer must also have fields named state_name and pop1990. You can adjust the code to work with another layer or other fields if needed.
  3. The sample will create a new table called SumCounties in the workspace of the input layer. You can adjust the code to create a different output table.
  4. Run the sample.
Public Sub CreateSummaryTable()
  
  On Error GoTo EH
  
  Dim pDoc As IMxDocument
  Set pDoc = ThisDocument
  Dim pMap As IMap
  Set pMap = pDoc.FocusMap

  ' Find the layer named US_Counties
  Dim pLayer As ILayer
  Dim pFeatLayer As IFeatureLayer
  Dim intCount As Integer
  For intCount = 0 To pMap.LayerCount - 1
    Set pLayer = pMap.Layer(intCount)
    If TypeOf pLayer Is IFeatureLayer Then
      If pLayer.Name = "US_Counties" Then
        Set pFeatLayer = pLayer
        Exit For
      End If
    End If
  Next
  If pFeatLayer Is Nothing Then
    MsgBox "The US_Counties layer was not found"
    Exit Sub
  End If

  ' Get the workspace of the US_Counties layer
  Dim pFeatClass As IFeatureClass
  Dim pTable As ITable
  Dim pDataSet As IDataset
  Dim pWkSpDS As IDataset
  Dim pWkSpName As IName
  Set pFeatClass = pFeatLayer.FeatureClass
  Set pTable = pFeatClass
  Set pDataSet = pTable
  Set pWkSpDS = pDataSet.Workspace
  Set pWkSpName = pWkSpDS.FullName
  
  ' Set up the output table
  Dim pOutTabName As ITableName
  Dim pOutDatasetName As IDatasetName
  Set pOutTabName = New TableName
  Set pOutDatasetName = pOutTabName
  pOutDatasetName.Name = "SumCounties"
  Set pOutDatasetName.WorkspaceName = pWkSpName
  
  ' Make sure there is a field called state_name in the layer
  If pTable.FindField("state_name") = -1 Then
    MsgBox "There must be a field named state_name in US_Counties"
    Exit Sub
  End If
  
  ' Perform the summarize. Note the summary fields string (minimum.state_name ...)
  ' below. This is a comma-delimited string that lists the generated summary
  ' fields. Each field must start with a keyword, and be followed by .fieldName,
  ' where fieldName is the name of a field in the original table.
  '
  ' If you specify the Shape field, you must use the keyword 'Dissolve'. This
  ' is not used below since we are creating a non-spatial summary table.
  Dim pGeoProc As IBasicGeoprocessor
  Dim pSumTable As ITable
  Set pGeoProc = New BasicGeoprocessor
  Set pSumTable = pGeoProc.Dissolve(pTable, False, "state_name", _
  "Minimum.state_name, Count.state_name, Sum.pop1990, Average.pop1990," & _
  "Minimum.pop1990, Maximum.pop1990, StdDev.pop1990, Variance.pop1990", _
  pOutDatasetName)
    
  ' add the table to map
  Dim pStTab As IStandaloneTable
  Set pStTab = New StandaloneTable
  Set pStTab.Table = pSumTable
  Dim pStTabColl As IStandaloneTableCollection
  Set pStTabColl = pMap
  pStTabColl.AddStandaloneTable pStTab

  ' Refresh the TOC
  pDoc.UpdateContents

  Exit Sub
EH:

  MsgBox Err.Number & " " & Err.Description

End Sub