Select to view content in your preferred language

confusing query filter

2848
14
11-05-2010 07:14 AM
Cut_EliaRahmi
Emerging Contributor
Dear all
I'm trying to learn th code below and compare with mine. I use vba,  but I get confusing in part of query. What is the code that appropriate for me to change "StateId"? Thanks a lot for everyone who help

Example:
Public Sub ZoomToState(StateId As String)
....
....
Dim pQueryFilter As IQueryFilter
Dim queryStr As String
  
queryStr = "Land_Unit='" & StateId & "'"
Set pActView = pMxDoc.activeView
Set pQueryFilter = New queryFilter
pQueryFilter.WhereClause = queryStr
Set pFCursor = pFClass.Search(pQueryFilter, True)

....
....

End Sub

My code for query:
Dim pQueryFilter As IQueryFilter
Set pQueryFilter = New queryFilter
If cboKabupaten.Visible = True Then
    pQueryFilter.WhereClause = "Land_Unit = '" & cboSatuanLahan.Text & "' And KABUPATEN = '" & cboKabupaten.Text & "'"
Else
    pQueryFilter.WhereClause = "Land_Unit = " & "'" & cboSatuanLahan.Text & "'"
End If
0 Kudos
14 Replies
Cut_EliaRahmi
Emerging Contributor
I followed an example from an arcobjects' book how to create selection layer, but I need to modify in part of query and zoom to feature, when I got stuck in query, I ask to this forum by the title confusing query filter. Now, the problem is solved. Thanks Richard, Thank you so much..

Regards,

Amie
0 Kudos
RichardFairhurst
MVP Alum
I followed an example from an arcobjects' book how to create selection layer, but I need to modify in part of query and zoom to feature, when I got stuck in query, I ask to this forum by the title confusing query filter. Now, the problem is solved. Thanks Richard, Thank you so much..

Regards,

Amie


I am glad it is working for you now.  Did you end up with just the first Sub or did you keep 2 Subs?  Did you end up keeping the line that stored the Definition Query whereclaues on the Soils layer or deleting it?  Did you leave the features selected or clear the selection at the end of the Sub?  Just curious to know what behaviors you ultimately were looking for.
0 Kudos
Cut_EliaRahmi
Emerging Contributor
I just use the first Sub, delete the Definition Query whereclause and clear the selection at the end of the Sub. Thanks Richard..
0 Kudos
hamadaltuwaijri
New Contributor
Hi,
I am having problem with my code I am trying to use zoom Modules in my project, BUT
after I run the code, it goes directly to the layer but with deleting all the information in Attribute table?

here is my code ?

Option Explicit

Private pMxDoc As IMxDocument
Private pFLayer As IFeatureLayer
Private pEnumLayer As IEnumLayer
Private pCurrentLayer As IFeatureLayer
Private pFClass As IFeatureClass
Private pFeature As IFeature

Public Sub ZoomToSDnumber(SDnumberId As String)

    Set pMxDoc = ThisDocument
    Set pFLayer = pMxDoc.FocusMap.Layer(0)
    Set pFClass = pFLayer.FeatureClass

    Dim pEnv As IEnvelope
    Dim pActView As IActiveView
    Dim pFCursor As IFeatureCursor
    Dim pQFilt As IQueryFilter
    Dim queryStr As String

    queryStr = "SD_NUMBER = '" & SDnumberId & "'"
    Set pActView = pMxDoc.ActiveView
    Set pQFilt = New QueryFilter
    pQFilt.WhereClause = queryStr
   
    Set pFCursor = pFClass.Search(pQFilt, True)
    Set pFeature = pFCursor.NextFeature
        If pFeature Is Nothing Then
            MsgBox "Check spelling and case", vbCritical + vbExclamation, "State Not Found!"
        Else
            pActView.Extent = pFeature.Shape.Envelope
            Set pEnv = pActView.Extent
            pEnv.Expand 1.1, 1.1, True
            pActView.Extent = pEnv
        End If
       
    Dim flDef As IFeatureLayerDefinition
    Set flDef = pFLayer
    flDef.DefinitionExpression = "SDNUMBER='" & SDnumberId & "'"
       
    pActView.Refresh
    pMxDoc.UpdateContents
   

End Sub


Public Function CreateSelLayer(SDnumberId As String)

    Set pMxDoc = ThisDocument
    Set pFLayer = pMxDoc.FocusMap.Layer(0)
   
    ''** Make a selection on the FeatureLayer
    Dim queryStr As String
    Dim qF As IQueryFilter
    Set qF = New QueryFilter
    queryStr = "SD_NUMBER='" & SDnumberId & "'"
    qF.WhereClause = queryStr
    Dim pFSel As IFeatureSelection
    Set pFSel = pFLayer
    pFSel.SelectFeatures qF, esriSelectionResultNew, False
   
    Dim pFDef As IFeatureLayerDefinition
    Set pFDef = pFLayer
   
   
    Dim SelFeatLayer As IFeatureLayer
    Set SelFeatLayer = pFDef.CreateSelectionLayer(StateId, True, "", "")
   
    With pMxDoc
        .AddLayer SelFeatLayer
        .FocusMap.MoveLayer SelFeatLayer, 2
        .FocusMap.ClearSelection
        .ActiveView.Refresh
        .UpdateContents
    End With
   
   


End Function
0 Kudos
RichardFairhurst
MVP Alum
I have corrected the definition query statement in bold where you misspelled the field name (you left out an underscore).  It should work with that correction.

Hi,
I am having problem with my code I am trying to use zoom Modules in my project, BUT
after I run the code, it goes directly to the layer but with deleting all the information in Attribute table?

here is my code ?

Option Explicit

Private pMxDoc As IMxDocument
Private pFLayer As IFeatureLayer
Private pEnumLayer As IEnumLayer
Private pCurrentLayer As IFeatureLayer
Private pFClass As IFeatureClass
Private pFeature As IFeature

Public Sub ZoomToSDnumber(SDnumberId As String)

    Set pMxDoc = ThisDocument
    Set pFLayer = pMxDoc.FocusMap.Layer(0)
    Set pFClass = pFLayer.FeatureClass

    Dim pEnv As IEnvelope
    Dim pActView As IActiveView
    Dim pFCursor As IFeatureCursor
    Dim pQFilt As IQueryFilter
    Dim queryStr As String

    queryStr = "SD_NUMBER = '" & SDnumberId & "'"
    Set pActView = pMxDoc.ActiveView
    Set pQFilt = New QueryFilter
    pQFilt.WhereClause = queryStr
    
    Set pFCursor = pFClass.Search(pQFilt, True)
    Set pFeature = pFCursor.NextFeature
        If pFeature Is Nothing Then
            MsgBox "Check spelling and case", vbCritical + vbExclamation, "State Not Found!"
        Else
            pActView.Extent = pFeature.Shape.Envelope
            Set pEnv = pActView.Extent
            pEnv.Expand 1.1, 1.1, True
            pActView.Extent = pEnv
        End If
        
    Dim flDef As IFeatureLayerDefinition
    Set flDef = pFLayer
    flDef.DefinitionExpression = "SD_NUMBER='" & SDnumberId & "'"        
    pActView.Refresh
    pMxDoc.UpdateContents
    

End Sub


Public Function CreateSelLayer(SDnumberId As String)

    Set pMxDoc = ThisDocument
    Set pFLayer = pMxDoc.FocusMap.Layer(0)
    
    ''** Make a selection on the FeatureLayer
    Dim queryStr As String
    Dim qF As IQueryFilter
    Set qF = New QueryFilter
    queryStr = "SD_NUMBER='" & SDnumberId & "'"
    qF.WhereClause = queryStr
    Dim pFSel As IFeatureSelection
    Set pFSel = pFLayer
    pFSel.SelectFeatures qF, esriSelectionResultNew, False
    
    Dim pFDef As IFeatureLayerDefinition
    Set pFDef = pFLayer
    
    
    Dim SelFeatLayer As IFeatureLayer
    Set SelFeatLayer = pFDef.CreateSelectionLayer(StateId, True, "", "")
    
    With pMxDoc
        .AddLayer SelFeatLayer
        .FocusMap.MoveLayer SelFeatLayer, 2
        .FocusMap.ClearSelection
        .ActiveView.Refresh
        .UpdateContents
    End With
    
    


End Function
0 Kudos