How to add a table to ArcMap


This sample shows how to add delimited text files and dBASE files to ArcMap. The first routine adds a text file to the focus map while the second adds a dBASE file to the focus map. The third routine is called by the other two. The process of adding a text file differs from adding a dBASE file only by the type of workspace factory accessed.

How to use

  1. Paste the code into VBA.
  2. Modify the code to specify the desired dBASE or text file name and location.
  3. Run either AddDBASEFile or AddTextFile macro.
[VBA]
Public Sub AddTextFile()
    
    ' Get the ITable from the geodatabase
    Dim pFact As IWorkspaceFactory
    Dim pWorkspace As IWorkspace
    Dim pFeatws As IFeatureWorkspace
    Dim pTable As ITable
    Set pFact = New TextFileWorkspaceFactory
    Set pWorkspace = pFact.OpenFromFile("d:\tables81\testing", 0)
    Set pFeatws = pWorkspace
    Set pTable = pFeatws.OpenTable("farms.txt")
    
    ' Add the table
    Add_Table_TOC pTable
    
End Sub

Public Sub AddDBASEFile()
    
    ' Get the ITable from the geodatabase
    Dim pFact As IWorkspaceFactory
    Dim pWorkspace As IWorkspace
    Dim pFeatws As IFeatureWorkspace
    Dim pTable As ITable
    Set pFact = New ShapefileWorkspaceFactory
    Set pWorkspace = pFact.OpenFromFile("d:\tables81\testing", 0)
    Set pFeatws = pWorkspace
    Set pTable = pFeatws.OpenTable("demog")
    
    ' add the table
    Add_Table_TOC pTable
    
End Sub

Private Sub Add_Table_TOC(pTable As ITable)
    
    Dim pDoc As IMxDocument
    Dim pMap As IMap
    Set pDoc = ThisDocument
    Set pMap = pDoc.FocusMap
    
    ' Create a new standalone table and add it
    ' to the collection of the focus map
    Dim pStTab As IStandaloneTable
    Set pStTab = New StandaloneTable
    Set pStTab.Table = pTable
    Dim pStTabColl As IStandaloneTableCollection
    Set pStTabColl = pMap
    pStTabColl.AddStandaloneTable pStTab
    
    ' Refresh the TOC
    pDoc.UpdateContents
    
End Sub