This example demonstrates how to convert a OLE DB table to an ArcSDE Geodatabase table.
How to use
- Paste the code into your VB6/VBA application.
- Call the procedure from within your application.
Sub Convert_OLEDB_Table()
'++ Convert an OLE DB table to a ArcSDE Geodatabase table
'++ Data source is an existing ODBC DSN connection to an MS Excel file
'++ OLE DB provider is MS OLE DB provider for ODBC Drivers
On Error GoTo ErrorHandler
Dim InTable As String, OutTable As String
Dim ConStr As String, InProg_id As String, OutProg_id As String
ConStr = "Provider=MSDASQL.1;Data Source=Excel_wks"
'++ Set the In and Out WorkspaceFactory Prog_ids
InProg_id = "esriDataSourcesOleDB.OLEDBWorkspaceFactory.1"
OutProg_id = "esriDataSourcesGDB.SDEWorkspaceFactory.1"
InTable = "codemog"
OutTable = "vtest.vtest.CODEMOG"
'++ Create the Source Workspace
Dim pWorkspaceIn As IWorkspaceName
Set pWorkspaceIn = New WorkspaceName
'++ Set the Source Workspace Properties
Dim pInPropset As IPropertySet
Set pInPropset = New PropertySet
pInPropset.SetProperty "CONNECTSTRING", ConStr
pWorkspaceIn.ConnectionProperties = pInPropset
pWorkspaceIn.WorkspaceFactoryProgID = InProg_id
'++ Assign input table name
Dim pInTableName As ITableName
Set pInTableName = New TableName
Dim pInDatasetName As IDatasetName
Set pInDatasetName = pInTableName
pInDatasetName.Name = InTable
Set pInDatasetName.WorkspaceName = pWorkspaceIn
'++ Create the Destination GDB workspace.
Dim pOutPropSet As IPropertySet
Set pOutPropSet = New PropertySet
Dim pWorkspaceOut As IWorkspaceName
Set pWorkspaceOut = New WorkspaceName
pWorkspaceOut.WorkspaceFactoryProgID = OutProg_id
With pOutPropSet
.SetProperty "Server", "fabio"
.SetProperty "Instance", "5152"
.SetProperty "Database", "vtest"
.SetProperty "user", "vtest"
.SetProperty "password", "go"
.SetProperty "version", "VTEST.EV1"
End With
pWorkspaceOut.ConnectionProperties = pOutPropSet
'++ Create the destination table object
Dim pOutDatasetName As IDatasetName
Dim pOutTableName As ITableName
Set pOutTableName = New TableName
Set pOutDatasetName = pOutTableName
Set pOutDatasetName.WorkspaceName = pWorkspaceOut
pOutDatasetName.Name = OutTable
'++ Open input table to get field definitions.
Dim pname As IName
Dim pTableIn As ITable
Set pname = pInTableName
Set pTableIn = pname.Open
'++ Validate the input table field names
Dim pInTableFields As IFields
Dim pOutTableFields As IFields
Dim pFieldCheck As IFieldChecker
Dim outEnumFieldError As esriGeoDatabase.IEnumFieldError
Set pFieldCheck = New FieldChecker
Set pInTableFields = pTableIn.Fields
pFieldCheck.Validate pInTableFields, outEnumFieldError, pOutTableFields
'++ if there are no field errors reported, proceed to convert
If outEnumFieldError Is Nothing Then
Dim pOleDBToGDB As IFeatureDataConverter
Set pOleDBToGDB = New FeatureDataConverter
pOleDBToGDB.ConvertTable pInDatasetName, Nothing, pOutDatasetName, pOutTableFields, "", 1000, 0
End If
Exit Sub
ErrorHandler:
MsgBox "Convert_OLEDB_Table" & Err.Number & Err.Description
End Sub
'++ To convert an OLE DB table to a Personal Geodatabase table
'++ Change the OutProg_id to "esriDataSourcesGDB.AccessWorkspaceFactory.1"
'++ and set the destination workspace property "DATABASE" as follows
'++ pOutPropSet.SetProperty "DATABASE", "d:\data\us_states.mdb"
'++