Using ArcObjects to get Unique Values from a Table

date:2007-02-25 16:35

A common requirement in many user dialogs is to display a list of unique values from a table, in order to delete or select records. While it is often easier to use Microsoft’s data objects, I try to use ArcObjects as the relevant libraries will always be installed on a user’s machine. The function will normally part of a larger project and I don’t like mixing the two methods to retrieve values from tables. I usually reuse a custom class for dealing with different types of geodatabases so that if a client changes from Access to SQL Server code changes will be minimal. Keeping up with changes to ESRI’s data access objects and Microsoft’s in the same project could get nasty..

There is a VBA / VB6 sample on how to get unique values at the EDN Site - but no .NET equivalent, hence my code sample below. There are no major changes except now a standard System.Collections enumerator is used rather than the ESRI IEnumVariantSimple.

    Public Sub ListUniqueRecords()

        Dim pMyTable As ITable

        Dim pCurs As ICursor = Nothing

        Dim intFieldIdx As Integer

        Dim pDataStatistics As IDataStatistics

        Dim pEnumVar As IEnumerator

        Dim pWorkspaceFactory As IWorkspaceFactory

        Dim pWorkspace As IWorkspace

        Dim pFeatWorkSpace As IFeatureWorkspace

        Dim strMyField As String = “VAL”


            pWorkspaceFactory = New AccessWorkspaceFactory

            pWorkspace = pWorkspaceFactory.OpenFromFile(“C:\MyPath\MyGDB.mdb”, 0)

            pFeatWorkSpace = CType(pWorkspace, IFeatureWorkspace)

            pMyTable = pFeatWorkSpace.OpenTable(“MyTableName”)

            intFieldIdx = pMyTable.FindField(strMyField)

            pCurs = pMyTable.Search(Nothing, True)

            pDataStatistics = New DataStatistics

            pDataStatistics.Field = strMyField

            pDataStatistics.Cursor = pCurs

            pEnumVar = CType(pDataStatistics.UniqueValues, IEnumerator)

            Do Until pEnumVar.MoveNext = False



        Catch ex As Exception



            ‘clean up

            pCurs = Nothing

            pWorkspace = Nothing

        End Try

    End Sub



1. ykon **

it looks like a very handy solution. Thanks for the tip! I tried to

replicate it but got an error about ‘clr’ not being a recognised module. Did I forgot something? | Cheers.


2. Ishara **

Brilliant - thanks.

Add Comment