Select to view content in your preferred language

sorting ascending value

809
2
12-04-2010 03:01 AM
vellaapriano
Emerging Contributor
Dear All,
I???m writing code with vba to insert field values from attribute table to combo box, the code runs well but the values aren???t sorted ascending. The values in cboValue are depending on the selected field name in cboField. Could someone please help me how to sort the disordered value to ascending?
Thanks

Here is my code:

Private Sub cboField_Change()
Dim theDocument As IMxDocument
Set theDocument = ThisDocument
Dim featLayer As IFeatureLayer
Dim featClass As IFeatureClass
Dim featCursor As IFeatureCursor
Dim pFeature As IFeature

Set featLayer = theDocument.FocusMap.Layer(2)

If featLayer Is Nothing Then Exit Sub

Set featClass = featLayer.FeatureClass
Set featCursor = featClass.Search(Nothing, False)
Set pFeature = featCursor.NextFeature

cboValue.Clear
Do While Not pFeature Is Nothing
Dim strValue As String
Dim existValue As Boolean
Dim j As Integer

strValue = UCase$(pFeature.Value(pFeature.fields.FindField(cboField.Text)))

'Checking redudance value from field
existValue = False
For j = 0 To cboValue.ListCount - 1
    If strValue = cboValue.List(j) Then
        existValue = True
        Exit For
    End If
Next

If existValue = False Then
    cboValue.AddItem strValue
End If

Set pFeature = featCursor.NextFeature
Loop
End Sub
0 Kudos
2 Replies
JamesCrandall
MVP Alum
You could add your values to a List(Of T), sort that, then set your ComboBox's Datasource to this list.

Dim cboValueList As New List(Of String)

Set featCursor = featClass.Search(Nothing, False)
Set pFeature = featCursor.NextFeature
Do While Not pFeature Is Nothing

 cboValueList.Add(UCase$(pFeature.Value(pFeature.fields.FindField(cb oField.Text))))

 Set pFeature = featCursor.NextFeature
Loop

cboValueList.Sort()
ComboBox1.DataSource = cboValueList
0 Kudos
JeffreyHamblin
Occasional Contributor
How about using a queryfilter when getting your cursor?

So instead of passing it nothing like:
Set featCursor = featClass.Search(Nothing, False)

    ' Set an IQueryFilter to use ORDER BY to sort in YOUR_SORT_FIELD order
    
    Dim pQueryFilter As IQueryFilter
    Set pQueryFilter = New QueryFilter
    Dim pQueryFilterDefinition As IQueryFilterDefinition
    Set pQueryFilterDefinition = pQueryFilter
    
    pQueryFilter.SubFields = "*"
    pQueryFilter.WhereClause = ""
    pQueryFilterDefinition.PostfixClause = "ORDER BY YOUR_SORT_FIELD "
    
    ' Get cursor from IFeatureLayer, which respects the layer's definition query
    ' Set pCursor = pFeatureLayer.Search(pQueryFilter, False)
    ' -OR-
    ' Get cursor from IFeatureClass, which DOES NOT use the layer's definition query
    ' and will return all features.
    Set pCursor = pFeatureClass.Search(pQueryFilter, False)
0 Kudos