Select to view content in your preferred language

Splitting Layer by Unique Values to New Shapefiles

616
1
07-23-2010 06:08 AM
ChristopherFarah
Emerging Contributor
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
0 Kudos
1 Reply
ChristopherFarah
Emerging Contributor
The code is correct.

1. The problem was that the output folder has to be the same as the folder where the input table was located.

2. Also, the input table has to be a stand alone dbf table created in arccatalog and not an excel file or a dbf automatiacally created when something is saved as a shapefile.

3. To get the stand alone dbf from an existing shapfile you can open the shapfile attribute table, hit options, then export. If you want your output data to be in the form of a shapefile you must have xy data in the original table and then hit add xy data to the output and export as shape.

Hope this helps people with a similar issue.
0 Kudos