Hi,
I am trying to split a layer by it unique values and I am having some trouble. I think the trouble is coming with function of exporting to a new table . Can anyone help please? Here is the code that I have been using thus far. The error message takes me to the line:
pExportOp.ExportTable pInDSName, pQryFltr, Nothing, pOutDSName, Application.hWnd
Public Sub SplitTable()
' ---- You'll need to change these values ----
Const FIELD_NAME = "Street Address"
Const OUTPUT_FOLDER = "C:\"
Const OUTPUT_FILE_NAME = "MySplitTable"
' --------------------------------------------
Dim pMxDoc As IMxDocument
Dim pSATbl As IStandaloneTable
Dim pTbl As ITable
Dim pDataset As IDataset
Dim pSQLSyntax As ISQLSyntax
Dim pDataStats As IDataStatistics
Dim pEnumVar As IEnumVariantSimple
Dim pQryFltr As IQueryFilter
Dim sFldDelPrefix As String
Dim sFldDelSuffix As String
Dim lFldIdx As Long
Dim sWhere As String
Dim vNextVal As Variant
Dim lFileNum As Long
' Ensure we've selected a single table
Set pMxDoc = ThisDocument
If pMxDoc.SelectedItem Is Nothing Then
MsgBox "Please Select A Single Table To Split", vbExclamation
Exit Sub
End If
If Not (TypeOf pMxDoc.SelectedItem Is IStandaloneTable) Then
MsgBox "Please Select A Single Table To Split", vbExclamation
Exit Sub
End If
Set pSATbl = pMxDoc.SelectedItem
Set pTbl = pSATbl.Table
' Ensure we've specified an existing field
lFldIdx = pTbl.FindField(FIELD_NAME)
If lFldIdx = -1 Then
MsgBox "Can't Find Field: " + FIELD_NAME + " In The Selected Table", vbExclamation
Exit Sub
End If
Set pDataset = pTbl
Set pSQLSyntax = pDataset.Workspace
sFldDelPrefix = pSQLSyntax.GetSpecialCharacter(esriSQL_DelimitedIdentifierPrefix)
sFldDelSuffix = sFldDelims + pSQLSyntax.GetSpecialCharacter(esriSQL_DelimitedIdentifierSuffix)
lFileNum = 0
Set pQryFltr = New QueryFilter
' Get the unique values for the field
Set pDataStats = New DataStatistics
Set pDataStats.Cursor = pTbl.Search(Nothing, False)
pDataStats.Field = FIELD_NAME
Set pEnumVar = pDataStats.UniqueValues
' Loop thru the uniqe values and export all matching records to a different table
' eg. MySplitTable_1, MySplitTable_2, MySplitTable_3, etc..
vNextVal = pEnumVar.Next
While Not (IsEmpty(vNextVal))
' Build the query
sWhere = sFldDelPrefix + FIELD_NAME + sFldDelSuffix + " = "
If pTbl.Fields.Field(lFldIdx).Type = esriFieldTypeString Then
sWhere = sWhere + "'" + CStr(vNextVal) + "'"
Else
sWhere = sWhere + CStr(vNextVal)
End If
pQryFltr.whereClause = sWhere
lFileNum = lFileNum + 1
' Call the export sub
ExportDBFTable pTbl, pQryFltr, OUTPUT_FOLDER, OUTPUT_FILE_NAME + "_" + CStr(lFileNum)
' Get the next unique field value
vNextVal = pEnumVar.Next
Wend
MsgBox CStr(lFileNum) + " Tables Created", vbInformation
End Sub
Private Sub ExportDBFTable(pInTbl As ITable, pQryFltr As IQueryFilter, sOutPath As String, sOutName As String)
' Exports the specified table using the query
Dim pDataset As IDataset
Dim pInDSName As IDatasetName
Dim pOutDSName As IDatasetName
Dim pWSName As IWorkspaceName
Dim pExportOp As IExportOperation
' Get the DatasetName of the input table
Set pDataset = pInTbl
Set pInDSName = pDataset.FullName
' Create a workspaceName for the output
Set pWSName = New WorkspaceName
pWSName.WorkspaceFactoryProgID = "esriDataSourcesGDB.ShapefileWorkspaceFactory"
pWSName.PathName = sOutPath
' Create the output DatasetName
Set pOutDSName = New TableName
Set pOutDSName.WorkspaceName = pWSName
pOutDSName.Name = sOutName
' Execute the Export operation
Set pExportOp = New ExportOperation
pExportOp.ExportTable pInDSName, pQryFltr, Nothing, pOutDSName, Application.hWnd
End Sub
Thanks,
Nimesh Patel
MIHGH