philjw

Select by Attribute Combobox Help

Discussion created by philjw on Aug 3, 2011
Latest reply on Aug 4, 2011 by philjw
Im trying to write code for a combobox toolbar which will populate itself with unique features and then select by attribute the correct feature.  Much like the code discussed here http://forums.esri.com/Thread.asp?t=251246&c=93&f=992

My code looks like this but I need help figuring it out.  Any help?

rivate Sub UserForm_Initialize()

GetDistinctValues UserForm2.ComboBox1, "TOWNSHIP"
GetDistinctValues UserForm2.ComboBox2, "RANGE"
GetDistinctValues UserForm2.ComboBox3, "SECTION"

End Sub
Sub GetDistinctValues(cboTemp As ComboBox, strFieldName As String)

Dim pMxDoc As IMxDocument
Dim pFeatureLayer As IFeatureLayer

Set pMxDoc = ThisDocument
Set pFeatureLayer = pMxDoc.FocusMap.Layer(1)

Dim pDataStatistics As IDataStatistics
Set pCursor = pFeatureLayer.Search(Nothing, False)
Set pDataStatistics = New DataStatistics
pDataStatistics.Field = strFieldName
Set pDataStatistics.Cursor = pCursor
Set pEnumVariantSimple = pDataStatistics.UniqueValues

Dim value As Variant
value = pEnumVariantSimple.Next


' setting FCursor
Set pFCursor = pFClass.Search(Nothing, False)
Set pFeature = pFCursor.NextFeature

' determine the unique values of user selected fieldname
Dim iFieldNum As Integer
iFieldNum = pFClass.Fields.FindField(strFieldName) 'is this where I put my field name?

Do Until pFeature Is Nothing
pUniqueRenderer.AddValue pFeature.value(iFieldNum), "", Nothing
Set pFeature = pFCursor.NextFeature
Loop

' loop over the pUniqueRenderer and list values to listbox
Dim iCount As Integer
cboTemp.Clear
For iCount = 0 To pUniqueRenderer.ValueCount - 1
cboTemp.AddItem pUniqueRenderer.value(iCount)
Next iCount

End Sub


Public Sub SelectMapFeatures()
  Dim pMxDoc As IMxDocument
  Dim pMap As IMap
  Dim pActiveView As IActiveView
  Dim pFeatureLayer As IFeatureLayer
  Dim pFeatureSelection As IFeatureSelection
  Dim pQueryFilter As IQueryFilter
 
  Set pMxDoc = Application.Document
  Set pMap = pMxDoc.FocusMap
  Set pActiveView = pMap
 
  'For simplicity sake let's use the first layer in the map
  If Not TypeOf pMap.Layer(1) Is IFeatureLayer Then Exit Sub
  Set pFeatureLayer = pMap.Layer(1)
  Set pFeatureSelection = pFeatureLayer
 
  'Create the query filter
  Set pQueryFilter = New QueryFilter
  pQueryFilter.SubFields = "TOWNSHIP,RANGE,SECTION"
  pQueryFilter.WhereClause = "TOWNSHIP = '" & UserForm2.ComboBox1.value & "' or RANGE = '" & UserForm2.ComboBox2.value & "' or SECTION = " & UserForm2.ComboBox3.value & "'"
 
  'Invalidate only the selection cache
  'Refresh the old selection if any to erase it
  pActiveView.PartialRefresh esriViewGeoSelection, Nothing, Nothing
 
  'Perform the selection
  'Debug.Print SelectFeature
  pFeatureSelection.SelectFeatures pQueryFilter, esriSelectionResultNew, False
  'pFeatureSelection.CombinationMethod = esriSelectionResultNew
 
  'Refresh again to draw the new selection
  pActiveView.PartialRefresh esriViewGeoSelection, Nothing, Nothing
   
End Sub

Outcomes