I'm looking to delete a standalone table that has been added to the arcmap document from an access database. I only want to delete it from the TOC, not from the file and I can't seem to get it to work.

Here is my current code:

        IMxDocument mxDocument;
        mxDocument = (IMxDocument)ArcMap.Application.Document;


        ////IWorkspaceFactory workspaceFactory;
        ////workspaceFactory = new AccessWorkspaceFactory();
        IMxDocument mxdoc = ArcMap.Application.Document as IMxDocument;
        IMap map = mxdoc.FocusMap;

        // Get the table 
        IStandaloneTableCollection stTableCollection = map as IStandaloneTableCollection;
        IStandaloneTable standaloneTable = null;
        ITable table = null;                    

        IFields fields;
        fields = table.Fields;

        ITableCollection tableColl = map as ITableCollection;

        table = (ITable)map.Layer[0];

        int x = map.LayerCount;
        for (int i = 0; (i < x); i++)
            if (map.get_Layer(i).Name == name)
                table = (ITable)map.get_Layer(i);

  • You're getting a table collection but not using it, straight away you're then going through the map with layers as tables.. How do you know if you've got the right table? Do you just want to remove any tables? There's a lot of that code there that just doesn't relate to the task you're trying to achieve. – Michael Stimson Mar 23 '15 at 22:58

Based on this Very old help you want to use your IStandaloneTableCollection

        IStandaloneTableCollection pColl = ArcMap.Document.FocusMap as IStandaloneTableCollection;
        IStandaloneTable pThisTable = pColl.StandaloneTable[0]; // the first table
        ArcMap.Document.UpdateContents(); //update the TOC
        //ArcMap.Document.ActivatedView.Refresh(); // refresh the view, if necessary.

or if you want to remove a table based on its name:

        for (int I = 0; I < pColl.StandaloneTableCount; I++)
            pThisTable = pColl.StandaloneTable[I];
            if (pThisTable.Name == "Name that I'm expecting")
                break; // important: get out now, the index is no longer valid
  • Michael, out of curiosity, what would you say is the best resource for ArcObjects you've come across? – ianbroad Mar 23 '15 at 23:12
  • GIS.SE without a doubt. The second best is Google, there's so many samples and helps the best way to find anything is just to Google what you're trying to do. The ArcObjects help is counter-intuitive: you need to know what objects you need before you can get help on them but occasionally there's samples that are quite valuable. – Michael Stimson Mar 23 '15 at 23:24

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.