How To: Merge two feature layers in VBA using IBasicGeoprocessor


IBasicGeoprocessor contains five methods: Dissolve, Merge, Clip, Intersect and Union. Access these methods using VBA or VB.

Merge combines data from two or more layers of the same geometry type and creates a new feature class.


The syntax for Merge is:

Set variable = object.Merge (Tables, fieldsTable, outputName)


   variable - is a reference to an object that implements IFeatureClass.
object - is an object expression that evaluates to an IBasicGeoprocessor object.
Tables - is an IArray object of the tables to be merged.
fieldsTable - is an ITable object that specifies the table used to determine the fields in the output feature class.
outputName - is an IFeatureClassName object.

This VBA example merges two feature layers.

Public Sub Merge()

' Get the first layer in the map
Dim pMxDoc As IMxDocument
Set pMxDoc = ThisDocument
Dim pLayer As ILayer
Set pLayer = pMxDoc.FocusMap.Layer(0)
Dim pFeatLayer As IFeatureLayer
Set pFeatLayer = pLayer
Dim pFirstFeatClass As IFeatureClass
Set pFirstFeatClass = pFeatLayer.FeatureClass

' Get the first layer’s table
' Use the Itable interface from the Layer (not from the FeatureClass)
' This table defines which fields are to be used in the output
Dim pFirstTable As ITable
Set pFirstTable = pLayer

' Get the second layer and its table
' Use the Itable interface from the Layer (not from the FeatureClass)
Set pLayer = pMxDoc.FocusMap.Layer(1)
Dim pSecondTable As ITable
Set pSecondTable = pLayer

' Error checking
If pFirstTable Is Nothing Then
MsgBox "Table QI failed"
Exit Sub
End If

If pSecondTable Is Nothing Then
MsgBox "Table QI failed"
Exit Sub
End If

' Define the output feature class name and shape type
Dim pFeatClassName As IFeatureClassName
Set pFeatClassName = New FeatureClassName

With pFeatClassName
.FeatureType = esriFTSimple
.ShapeFieldName = "Shape"
.ShapeType = pFirstFeatClass.ShapeType
End With

' Set the output location and feature class name
Dim pNewWSName As IWorkspaceName
Set pNewWSName = New WorkspaceName

With pNewWSName
.WorkspaceFactoryProgID = "esriCore.ShapefileWorkspaceFactory.1"
.PathName = "C:\temp"
End With

Dim pDatasetName As IDatasetName
Set pDatasetName = pFeatClassName
pDatasetName.Name = "Merge_result"

Set pDatasetName.WorkspaceName = pNewWSName

' Build the input set/array – these are the layers to be merged
Dim inputArray As IArray
Set inputArray = New esriCore.Array ' in ArcGIS 9.0 and newer versions replace with '= New esriSystem.Array'
inputArray.Add pFirstTable
inputArray.Add pSecondTable

' Perform the merge
Dim pBGP As IBasicGeoprocessor
Set pBGP = New BasicGeoprocessor
Dim pOutputFeatClass As IFeatureClass
Set pOutputFeatClass = pBGP.Merge(inputArray, pFirstTable, pFeatClassName)

' Add the output to the map
Dim pOutputFeatLayer As IFeatureLayer
Set pOutputFeatLayer = New FeatureLayer
Set pOutputFeatLayer.FeatureClass = pOutputFeatClass
pOutputFeatLayer.Name = pOutputFeatClass.AliasName
pMxDoc.FocusMap.AddLayer pOutputFeatLayer

End Sub

Related Information