Select to view content in your preferred language

How do you add a stand alone table to the map document using ArcObjects?

4665
6
11-27-2010 08:32 AM
GregRieck
Regular Contributor
Hello, Does any one know how to do the following using ArcObjects:

1) determine if a stand-alone table is already in the map document
2) determine if a stand-alone table is already opened in the map document
3) add and/or open a stand-alone table based on above results
4) make the stand-alone table the selected table, i.e. has focus after being added to the map document.

Thank You,
Greg

Here is what I've been able to work up so far, but it doesn't add the table to the map document...

IStandaloneTable addThisTable = null;
IFeatureWorkspace fws = Editor3.EditWorkspace as IFeatureWorkspace;
if (fws != null)
{
  addThisTable = fws.OpenTable("TableToBeAdded") as IStandaloneTable;
  if (addThisTable  != null)
    return Result.Success();
}
bool isFound = false;
IEnumTableProperties pEnumTP = MxDoc.TableProperties.IEnumTableProperties;
pEnumTP.Reset();
ITableProperty3 pTP3 = pEnumTP.Next() as ITableProperty3;
while (pTP3 != null)
{
  if (pTP3.StandaloneTable != null)
  {
    if (pTP3.StandaloneTable.Name.ToLower() == "TableToBeAdded")
    {
      isFound = true;
      break;
    }
  }
  pTP3 = pEnumTP.Next() as ITableProperty3;
}
if (!isFound)
{
  pTP3 = new TableProperty() as ITableProperty3;
  pTP3.StandaloneTable = addThisTable;
  pTP3.SelectedTable = true;
  MxDoc.TableProperties.Add(pTP3 as ITableProperty);
}
0 Kudos
6 Replies
DuncanHornby
MVP Notable Contributor
Greg,

To add or find a table in the map document you need to be using the  IStandaloneTableCollection
Interface. You can loop with this to compare names of tables in the collection. There is no "find the table with this name" function you must enumerate the collection.

Duncan
0 Kudos
GregRieck
Regular Contributor
Greg,

To add or find a table in the map document you need to be using the  IStandaloneTableCollection
Interface. You can loop with this to compare names of tables in the collection. There is no "find the table with this name" function you must enumerate the collection.

Duncan


Great, thanks for the tip. Do you have any sample code for working with IStandaloneTableCollection?

Greg
0 Kudos
DuncanHornby
MVP Notable Contributor
Greg,

Here is a simple bit of code in VBA to get you going.

Sub test()
    ' Get document
    Dim pMXDocument As IMxDocument
    Set pMXDocument = ThisDocument
    
    ' Get Map
    Dim pMap As IMap
    Set pMap = pMXDocument.FocusMap
    
    ' Get table collection
    Dim pStandaloneTableCollection As IStandaloneTableCollection
    Set pStandaloneTableCollection = pMap
    
    ' Get a count of tables
    Dim n As Integer
    Let n = pStandaloneTableCollection.StandaloneTableCount
    
    ' Step through each table and print name
    Dim pStandaloneTable  As IStandaloneTable
    Dim i As Integer
    For i = 0 To (n - 1)
        Set pStandaloneTable = pStandaloneTableCollection.StandaloneTable(i)
        Debug.Print pStandaloneTable.Name
    Next i
End Sub


Duncan
0 Kudos
GregRieck
Regular Contributor
Duncan,

Thank you, already had that part working. I should have been more precise with my question. I was having issues "adding" the standalone table to the map document. I found that you have to call UpdateContents on the map document to get the new table to appear. Now all I have left is to actually open the newly added table and make sure that table has focus.

Greg


Greg,

Here is a simple bit of code in VBA to get you going.

Sub test()
    ' Get document
    Dim pMXDocument As IMxDocument
    Set pMXDocument = ThisDocument
    
    ' Get Map
    Dim pMap As IMap
    Set pMap = pMXDocument.FocusMap
    
    ' Get table collection
    Dim pStandaloneTableCollection As IStandaloneTableCollection
    Set pStandaloneTableCollection = pMap
    
    ' Get a count of tables
    Dim n As Integer
    Let n = pStandaloneTableCollection.StandaloneTableCount
    
    ' Step through each table and print name
    Dim pStandaloneTable  As IStandaloneTable
    Dim i As Integer
    For i = 0 To (n - 1)
        Set pStandaloneTable = pStandaloneTableCollection.StandaloneTable(i)
        Debug.Print pStandaloneTable.Name
    Next i
End Sub


Duncan
0 Kudos
DuncanHornby
MVP Notable Contributor
Greg,

Here is the VBA code for loading a table into the map document:

Sub test2()
    ' Get document
    Dim pMXDocument As IMxDocument
    Set pMXDocument = ThisDocument
    
    ' Get Map
    Dim pMap As IMap
    Set pMap = pMXDocument.FocusMap
    
    ' Get table collection
    Dim pStandaloneTableCollection As IStandaloneTableCollection
    Set pStandaloneTableCollection = pMap
    
    ' Create workspacefactory (for opening up dBase files)
    Dim pWorkspaceFactory As IWorkspaceFactory
    Set pWorkspaceFactory = New ShapefileWorkspaceFactory
    Dim pFeatureWorkSpace As IFeatureWorkspace
    Set pFeatureWorkSpace = pWorkspaceFactory.OpenFromFile("C:\Temp", 0)
    
    ' Get dBase table
    Dim pTable As ITable
    Set pTable = pFeatureWorkSpace.OpenTable("testtable") ' note no .dbf
    
    ' Create StandaloneTable
    Dim pStandaloneTable  As IStandaloneTable
    Set pStandaloneTable = New StandaloneTable
    Set pStandaloneTable.Table = pTable
    pStandaloneTable.Name = "My Table"
    
    ' Add table to Map
    pStandaloneTableCollection.AddStandaloneTable pStandaloneTable
    pMXDocument.UpdateContents
End Sub


Duncan
0 Kudos
GregRieck
Regular Contributor
Thank You Duncan for the help. I was able to get it working. I also used ITableWindow to open the table that was added using the example you provided. The trick with ITableWindow is to point it to the Application.

ITableWindow tabwindow = new TableWindow();
tabwindow.Application = Application;
tabwindow.Table = Table;
tabwindow.Show(true);


Greg
0 Kudos