Select By Attributes and Zoom to Selected Features

402
3
10-06-2011 04:56 PM
LaurenFagg
Occasional Contributor
hi all,

I'm just starting out in scripting and appreicate any help that I can get with this. I am trying to build a simple tool that will take text from two text boxes, enter them into an SQL query and hence select attributes from a "Cadastre - Parcels" Layer in my .mxd

The code I've written so far is this:

 Private Sub GoButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles GoButton.Click


        Dim pMXDoc As ESRI.ArcGIS.ArcMapUI.IMxDocument
        pMXDoc = My.ArcMap.Document
        Dim pMap As ESRI.ArcGIS.Carto.IMap
        pMap = pMXDoc.FocusMap
        Dim pFeatureLayer As ESRI.ArcGIS.Carto.IFeatureLayer
        Dim pActiveView As ESRI.ArcGIS.Carto.IActiveView
        pActiveView = pMap
        Dim pFeatureSelection As ESRI.ArcGIS.Carto.IFeatureSelection
        Dim LayerName As String
        LayerName = "Cadastre - Parcels"



        For i = 0 To pMap.LayerCount - 1

            If UCase(pMap.Layer(i).Name.Trim()) = UCase(LayerName.Trim()) Then
                pFeatureLayer = pMap.Layer(i)
                Exit For
            End If
        Next

        'Get attributes from the textboxes
        Dim AttPlanNumber As String
        Dim AttLotNumber As String
        Dim SearchStatement As String

        AttPlanNumber = txtDPNumber.Text
        AttLotNumber = txtLotNumber.Text
        pFeatureSelection = pFeatureLayer


        Dim filter As New ESRI.ArcGIS.Geodatabase.QueryFilterClass()
        SearchStatement = "PLANNO = '" & AttPlanNumber & "' & LOTNO = '" & AttLotNumber & "'"
        filter.WhereClause = SearchStatement

        pActiveView.PartialRefresh(ESRI.ArcGIS.Carto.esriViewDrawPhase.esriViewGeoSelection, Nothing, Nothing)

        pFeatureSelection.SelectFeatures(filter, ESRI.ArcGIS.Carto.esriSelectionResultEnum.esriSelectionResultNew, False)

End Sub


unfortunately, when I run the tool I get this:

pFeatureSelection.SelectFeatures(filter, ESRI.ArcGIS.Carto.esriSelectionResultEnum.esriSelectionResultNew, False) -----Check the ErrorCode property of the exception to determine the HRESULT returned by the COM Object.

Where am I going wrong?

Thanks,
Lauren
0 Kudos
3 Replies
JamesCrandall
MVP Frequent Contributor
Hi there Lauren,

I was a little confused by your useage of the QueryFilterClass() as I am unsure if this goes along with the IMxDocument --- from what I see, it belongs with ArcMobile and or WebADF.  Of course, I could be reading that incorrectly.  Are you working with ArcMap for this?

In any event, I've taken some snippets from similar functionality that I use for some ArcMap customizations and added to your existing code you posted.  I pretty much left what you had, added to it and altered the WHERE clause portion (I noticed some minor syntax problems with what you had).

Anyway -- let me know if this works!

Take Care.


       
        Dim pMXDoc As ESRI.ArcGIS.ArcMapUI.IMxDocument
        pMXDoc = My.ArcMap.Document
        Dim pMap As ESRI.ArcGIS.Carto.IMap
        pMap = pMXDoc.FocusMap
        Dim pFeatureLayer As ESRI.ArcGIS.Carto.IFeatureLayer
        Dim pActiveView As ESRI.ArcGIS.Carto.IActiveView
        pActiveView = pMap
        Dim pFeatureSelection As ESRI.ArcGIS.Carto.IFeatureSelection
        Dim LayerName As String
        LayerName = "Cadastre - Parcels"

        For i = 0 To pMap.LayerCount - 1

            If UCase(pMap.Layer(i).Name.Trim()) = UCase(LayerName.Trim()) Then
                pFeatureLayer = pMap.Layer(i)
                Exit For
            End If
        Next

        'Get attributes from the textboxes
        Dim AttPlanNumber As String
        Dim AttLotNumber As String
        Dim SearchStatement As String

        AttPlanNumber = txtDPNumber.Text
        AttLotNumber = txtLotNumber.Text
        pFeatureSelection = pFeatureLayer

        'setup a QueryFilter and where clause
        'If both Plan and Lot number MUST match, then use this
        Dim str As String = "PLANNO = '" & AttPlanNumber & "' AND " & LOTNO = '" & AttLotNumber & "'"
        'If either Plan or Lot number can match, then use this
        Dim str As String = "PLANNO = '" & AttPlanNumber & "' OR " & LOTNO = '" & AttLotNumber & "'"

        Dim pQueryFilter As IQueryFilter
        pQueryFilter = New QueryFilter
        pQueryFilter.WhereClause = str

        pFeatureSelection.SelectFeatures(pQueryFilter, esriSelectionResultEnum.esriSelectionResultNew, False)

        
        ' Refresh the selection
        pMxDoc.ActiveView.PartialRefresh(esriViewDrawPhase.esriViewGeoSelection, Nothing, Nothing)

        
        'Zoom to the selected features
        Dim pSelectionSet As ISelectionSet
        pSelectionSet = pFeatureSelection.SelectionSet

        Dim pEnumGeom As Geometry.IEnumGeometry
        Dim pEnumGeomBind As IEnumGeometryBind

        pEnumGeom = New EnumFeatureGeometry
        pEnumGeomBind = pEnumGeom
        pEnumGeomBind.BindGeometrySource(Nothing, pSelectionSet)

        Dim pGeomFactory As Geometry.IGeometryFactory
        pGeomFactory = New Geometry.GeometryEnvironmentClass

        Dim pGeom As IGeometry
        pGeom = pGeomFactory.CreateGeometryFromEnumerator(pEnumGeom)

        pMxDoc.ActiveView.Extent = pGeom.Envelope
        pMxDoc.FocusMap.MapScale = 2500
        pMxDoc.ActiveView.PartialRefresh(esriViewDrawPhase.esriViewGeography, Nothing, Nothing)
        pMxDoc.ActiveView.Refresh()
0 Kudos
AlexanderGray
Occasional Contributor III
QueryFilterClass is the class that implements IQueryFilter.  The use of the suffix class is correct and I believe preferable.  The online help shows it such and the local dev help shows it in the header of the topic.  I use it code also.  Makes for strange looking code some times, a FeatureClass implements IFeature and a FeatureClassClass implements IFeatureClass.

The code looks ok but it depends on the type of database your data is in.  Shapefile, Personal geodatabase, file geodatabase, Oracle, SQLServer, they all have slightly different sql syntax.  I would suggest you print out the searchStatement before the select is done and you paste it in the select by attribute form in the ArcMap GUI.  If the query doesn't work in the GUI, it won't work in code.  It could be as simple as a quote not closed or having to use square brackets around the field names.

To get around syntax problems related to concatenation I prefer to use the string.format method, makes all the quotes and spaces needed immediately visible.
instead of
Dim str As String = "PLANNO = '" & AttPlanNumber & "' OR " & LOTNO = '" & AttLotNumber & "'"


Dim str As String = string.format("PLANNO = '{0}' OR LOTNO = '{1}'", _
                                                            AttPlanNumber, AttLotNumber )
0 Kudos
LaurenFagg
Occasional Contributor
Thankyou both so much - that new code worked a treat and Yes I can see that my search query syntax was a bit off. Now I just need to do some more reading to understand what's different and why it worked 🙂

I really appreciate your help.

~ Lauren
0 Kudos