Using ArcObjects to get Unique Values from a Table

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

3 views shared on this article. Join in...

  1. Janey says:

    Can you explain IEnumerator??? I’m getting an error when trying to use it.

  2. Janey says:

    I’m having trouble with the CTypes also. It seems like this code is for not VBA/VB6. Do you have a conversion??? It would help me out a lot! I’m trying to modify it to do a few things with a table that I’m adding to an ArcMap document. Thanks!!

Leave a Reply

Your email address will not be published. Required fields are marked *


You may use these tags : <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>