How To: Add feature classes to ArcMap using VBA


This sample VBA code adds coverage arcs to an ArcMap document. You can modify it to specify data models other than coverages. You may also add it to a subroutine or function.


  1. Use a WorkspaceFactory object to create a pointer to the location of the data. Each data model requires its own type of WorkspaceFactory object. Consult the Geodatabase object model diagram for the various types.
    '++ Create a pointer to the data's workspace:
    Dim pWorkspaceFact As IWorkspaceFactory
    Set pWorkspaceFact = New ArcInfoWorkspaceFactory
    Dim pWorkspace As IWorkspace
    Set pWorkspace = pWorkspaceFact.OpenFromFile("c:\geodata\covers", 0)
    Dim pFeatureWorkspace As IFeatureWorkspace
    Set pFeatureWorkspace = pWorkspace
    For SDE layers, the code must specify the SDE connection information using an IPropertySet interface.
  2. Open the dataset by pointing a FeatureDataset object at the workspace's OpenFeatureDataset method.
    Dim pDataset As IFeatureDataset
    Set pDataset = pFeatureWorkspace.OpenFeatureDataset("covername")
  3. Create a pointer to the desired feature class of the dataset using an IFeatureClassContainer with the ClassByName property.
    Dim pFeatureClassContainer As IFeatureClassContainer
    Set pFeatureClassContainer = pDataset
    Dim pFeatureClass As IFeatureClass
    Set pFeatureClass = pFeatureClassContainer.ClassByName ("arc")
  4. Create an instance of an IFeatureLayer object to contain a map reference to the IFeatureClass data object and set the IFeatureLayer name property to the string that should appear in the TOC.
    Dim pFeatureLayer As IFeatureLayer
    Set pFeatureLayer = New FeatureLayer
    Set pFeatureLayer.FeatureClass = pFeatureClass
    pFeatureLayer.Name = "Feature class name"
    pFeatureLayer.Visible = True
  5. Add the layer to the ArcMap document by using the document's FocusMap property to reference the active map and use its AddLayer method.
    Dim pMxDocument As IMxDocument
    Set pMxDocument = ThisDocument
    pMxDocument.FocusMap.AddLayer pFeatureLayer
    For a sample of using the IPropertySet interface to reference an SDE connection, click the ArcObjects Online link below, and expand: Samples > Geodatabase > Connecting > Connect to Enterprise Geodatabase (VB). 

Related Information