Open Table button for ArcMap

    Table of contents
    No headers

    How to open a the table window for the selected layer or standalone table in the table of contents in ArcMap. It provides the same functionality as the Open Attribute Table command on the layer context menu or the open command on the standalone table context menu (source)

    1. Add a new UIButtonControl to any toolbar.
    2. Right-click on the command and select view source, then paste the code below here.
    3. Make sure the names of the controls match, the sample assumes the command is called UIButtonControl1.
    4. Completely close VBA so that events on the UIButtonControl fire correctly.
    5. Select a layer or standalone table in the table of contents and click the command.
    6. The table window for the selected layer or standalone table appears.
    Private Sub UIButtonControl1_Click()
      Dim pMxDoc As IMxDocument
      Dim pUnknown As IUnknown
      Dim pLayer As ILayer
      Dim pStandaloneTable As IStandaloneTable
      Dim pTableWindow2 As ITableWindow2
      Dim pExistingTableWindow As ITableWindow
      Dim SetProperties As Boolean
      'Get the selected item from the current contents view
      Set pMxDoc = ThisDocument
      Set pTableWindow2 = New TableWindow
      Set pUnknown = pMxDoc.SelectedItem
      ' Determine the selected item's type
      ' Exit sub if item is not a feature layer or standalone table
      If TypeOf pUnknown Is IFeatureLayer Then 'A FeatureLayer
        Set pLayer = pUnknown
        Set pExistingTableWindow = _
        ' Check if a table already exists; if not create one
        If pExistingTableWindow Is Nothing Then
    pTableWindow2.Layer = pLayer SetProperties = True
    End If
    ElseIf TypeOf
    pUnknown Is IStandaloneTable Then ' A standalone table Set pStandaloneTable = pUnknown Set pExistingTableWindow = _ pTableWindow2.FindViaStandaloneTable(pStandaloneTable) ' Check if a table already exists; if not, create one If pExistingTableWindow Is Nothing Then
    pTableWindow2.StandaloneTable = pStandaloneTable SetProperties = True
    End If
    End If

    SetProperties Then pTableWindow2.TableSelectionAction = esriSelectFeatures pTableWindow2.ShowSelected = False pTableWindow2.ShowAliasNamesInColumnHeadings = True
    pTableWindow2.Application = Application Else
    pTableWindow2 = pExistingTableWindow End If ' Ensure Table Is Visible If Not pTableWindow2.IsVisible Then pTableWindow2.Show True

    End Sub

    Private Function
    UIButtonControl1_Enabled() As Boolean

    pDoc As IMxDocument Dim pMap As IMap Dim bolEnabled As Boolean
    pDoc = ThisDocument Set pMap = pDoc.FocusMap bolEnabled = True

    pSelItem As IUnknown Set pSelItem = pDoc.SelectedItem ' Disable if the selected item is nothing or if ' it is not a layer or table If pSelItem Is Nothing Then bolEnabled = False
    ElseIf Not
    (TypeOf pSelItem Is ILayer Or TypeOf pSelItem Is IStandaloneTable) Then bolEnabled = False
    End If
    UIButtonControl1_Enabled = bolEnabled End Function


        Send feedback