How to create a summary table


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 layermust also have fields named state_name and pop1990. You can adjust the code to work withanother layer or other fields if needed.
  3. The sample will create a new table called SumSample in the c:\temp directory.Modify the code to specify a different directory or file name.
  4. Run the sample.
[VBA]
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
    
    ' The output is set to SumSample.dbf file in c:\temp
    Dim pWkSpFactory As IWorkspaceFactory
    Dim pWkSp As IWorkspace
    ' Dim pWkSpDS As IDataset
    ' Dim pWkSpName As IWorkspaceName
    Dim pOutDSName As IDatasetName
    Set pWkSpFactory = New ShapefileWorkspaceFactory
    Set pWkSp = pWkSpFactory.OpenFromFile("c:\temp", 0)
    Set pWkSpDS = pWkSp
    Set pWkSpName = pWkSpDS.FullName
    Set pOutDSName = New TableName
    pOutDSName.Name = "SumSample"
    Set pOutDSName.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", _
                    pOutDSName)
    
    ' 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