How to add OLE DB table to ArcMap document


This example demonstrates how to add a table from an OLE DB data source to an ArcMap document.

How to use

    [VBA]
    Public Sub add_oledb_table()
        '++ add_oledb_table:  adds an OLE DB table to an ArcMap document.
        On Error GoTo EH
        
        '++ Provider = Microsoft® OLE DB provider for Oracle.
        '++ Data source in this case is an Oracle 8.1.5 database
        
        '++ Create and populate a new property set
        Dim pPropset As IPropertySet
        Set pPropset = New PropertySet
        pPropset.SetProperty "CONNECTSTRING", "Provider=MSDAORA.1;Data source=mydatabase;User ID=oledb;Password=oledb"
        
        '++ Create a new workspacefactory/workspace
        Dim pWorkspaceFact As IWorkspaceFactory
        Set pWorkspaceFact = New OLEDBWorkspaceFactory
        
        Dim pWorkspace As IWorkspace
        Set pWorkspace = pWorkspaceFact.Open(pPropset, 0)
        
        Dim pFeatWorkspace As IFeatureWorkspace
        Set pFeatWorkspace = pWorkspace
        
        '++ Get the datasets (names) in the workspace
        Dim pEnumDataset As IEnumDatasetName
        Set pEnumDataset = pWorkspace.DatasetNames(esriDTTable)
        
        '++ Create a new dataset object for the table you want to load
        Dim pDataset As IDatasetName
        Set pDataset = pEnumDataset.Next
        
        Do Until pDataset Is Nothing
            If pDataset.Name = "OLEDB.MY_TABLE" Then
                Exit Do
            End If
            Set pDataset = pEnumDataset.Next
        Loop
        
        '++ Create and open the new table object from the dataset name
        Dim pTable As ITable
        Set pTable = pFeatWorkspace.OpenTable(pDataset.Name)
        
        '++ Create a table collection and assign the new table to it
        Dim pStTab As IStandaloneTable
        Dim pStTabColl As IStandaloneTableCollection
        Dim pMap As IMap
        Dim mx As IMxDocument
        Set mx = ThisDocument
        Set pMap = mx.FocusMap
        Set pStTab = New StandaloneTable
        Set pStTab.Table = pTable
        Set pStTabColl = pMap
        pStTabColl.AddStandaloneTable pStTab
        
        '++ Update the document
        mx.UpdateContents
        
        '++ Create and open a new table window for the table
        Dim ptabWin As ITableWindow
        Set ptabWin = New TableWindow
        Set ptabWin.Table = pTable
        ptabWin.ShowAliasNamesInColumnHeadings = True
        Set ptabWin.Application = Application
        ptabWin.Show True
        
        Exit Sub
        
    EH:
        MsgBox Err.Description, vbInformation, "add_oledb_table"
        
    End Sub