Update fields for only selected features?

1595
5
12-08-2010 10:39 AM
ducksunlimited
New Contributor
HI All,

I am developing a customized attribute editing form in ArcMap for our users. The users will select a polygon and follow the steps in the form to update the field. The following codes worked but it will update the field for all rows. How can I modify the codes to let it update only selected polygons in ArcMap?

Appreciate the help in advance!

Russel
Private Sub cmdSave_Click()
    'Get a reference to the first layer in the map
    Dim pMxDoc As IMxDocument
    Set pMxDoc = ThisDocument
    
    Dim pMap As IMap
    Set pMap = pMxDoc.FocusMap
    
    Dim pFlayer As IFeatureLayer
    Set pFlayer = pMap.Layer(0)
    
    
    Dim pFSelection As IFeatureSelection
    Set pFSelection = pFlayer
    
    Dim pFClass As IFeatureClass
    Set pFClass = pFlayer.FeatureClass

    
    Dim pFCursor As IFeatureCursor
    Set pFCursor = pFClass.Update(Nothing, True)

    Dim pFeature As IFeature
    Set pFeature = pFCursor.NextFeature
        
    Do Until pFeature Is Nothing
        pFeature.Value(2) = txtClassCode.Text
        pFCursor.UpdateFeature pFeature
        Set pFeature = pFCursor.NextFeature
    Loop
    
End Sub
0 Kudos
5 Replies
GregRieck
Occasional Contributor III
Hi Russel,

One way is to return only the selected features and edit the features individually. Keep in mind this only returns the selected features that are editable.

      IEnumFeature selectedfeatures = Editor3.EditSelection;//IEditor3 used here
      selectedfeatures.Reset();
      IFeature feat = selectedfeatures.Next();
      while (feat != null)
      {
        feat.set_Value(intIndexOfFeatureBeingUpdated, NewValue);
        feat.store();
        feat = selectedfeatures.Next();
      }


Greg
0 Kudos
ducksunlimited
New Contributor
Hi Greg, thanks for your reply.

I am programming in ArcMap using VBA. The codes you provided can't be correctly compiled.

can you provide a VBA version? Thanks

Russel


Hi Russel,

One way is to return only the selected features and edit the features individually. Keep in mind this only returns the selected features that are editable.

      IEnumFeature selectedfeatures = Editor3.EditSelection;//IEditor3 used here
      selectedfeatures.Reset();
      IFeature feat = selectedfeatures.Next();
      while (feat != null)
      {
        feat.set_Value(intIndexOfFeatureBeingUpdated, NewValue);
        feat.store();
        feat = selectedfeatures.Next();
      }


Greg
0 Kudos
GregRieck
Occasional Contributor III
Russel,
Sorry, didn't realize it was VBA. I sent you c#. It's pretty much the same just have to use IEditor instead of IEditor3 and change the way you update the IFeature as set_value isn't available. You will need a hook to the application to get access to IEditor. Getting IEditor is well documented should you have problems with that.

Greg

Try this:

  Dim pID As New esriSystem.UID
  pID.Value = "esriEditor.Editor"
  Set pEditor = Application.FindExtensionByCLSID(pID)
  Dim pFeature As esriGeoDatabase.IFeature
  Dim pEnumFeature As esriGeoDatabase.IEnumFeature
  Set pEnumFeature = pEditor.EditSelection
  Set pFeature = pEnumFeature.Next
  Do Until pFeature Is Nothing
    pFeature.Value(pFeature.Fields.FindField("FieldToUpdate")) = NewValue
    Set pFeature = pEnumFeature.Next
  Loop
0 Kudos
KevinSurbella
New Contributor
Hello,

I would like to also edit a selected feature/record via ArcMap vba but I'm not having much luck with this code sample, see below.  I'm not getting an error, but the record selected is not updating.
 
Private Sub Update_Click()
   
    Dim pMxDoc As IMxDocument
    Dim pLayer As ILayer
    Dim pInFtrLyr As IFeatureLayer
    Dim pFtrSel As IFeatureSelection
    Dim pFeatureClass As IFeatureClass
    Dim pFields As IFields
    'Dim ii As Integer
    Dim pPoint As IPoint
    'Dim pSegColl As ISegmentCollection
    Dim pEnumLayer As IEnumLayer
    Dim pMap As IMap
   
    Set pMxDoc = ThisDocument
    Set pMap = pMxDoc.FocusMap
    Set pEnumLayer = pMap.Layers
   
    Set pLayer = pEnumLayer.Next
    Do Until pLayer Is Nothing
        If pLayer.Name = "Copy of Wells_test" Then
            Set pInFtrLyr = pLayer
        End If
        Set pLayer = pEnumLayer.Next
    Loop

    Set pFtrSel = pInFtrLyr
   
      If pFtrSel.SelectionSet.Count = 0 Then
         MsgBox "Please select one Well record", vbOKOnly
         Exit Sub
      ElseIf pFtrSel.SelectionSet.Count > 1 Then
         MsgBox "Please select one Well record", vbOKOnly
         Exit Sub
      Else
      If pFtrSel.SelectionSet.Count = 1 Then
    Set pPoint = pInFtrLyr.FeatureClass.GetFeature(pFtrSel.SelectionSet.IDs.Next).Shape
     End If
      End If

    Set pFeatureClass = pInFtrLyr.FeatureClass
       
    Dim pFSelection As IFeatureSelection
    Set pFSelection = pInFtrLyr
   
    Dim pFClass As IFeatureClass
    Set pFClass = pInFtrLyr.FeatureClass

    Dim pFCursor As IFeatureCursor
    Set pFCursor = pFClass.Update(Nothing, True)

  Dim pID As New esriSystem.UID
  pID.value = "esriEditor.Editor"
  Dim pEditor As IEditor
  Set pEditor = Application.FindExtensionByCLSID(pID)
  Dim pFeature As esriGeoDatabase.IFeature
  Dim pEnumFeature As esriGeoDatabase.IEnumFeature
  Set pEnumFeature = pEditor.EditSelection
  Set pFeature = pEnumFeature.Next
  Do Until pFeature Is Nothing
    pFeature.value(pFeature.Fields.FindField("Type")) = "update"
    Set pFeature = pEnumFeature.Next
  Loop

End Sub

Thank you for your help.
0 Kudos
PhyllisDavis
New Contributor II
I have an error on my pfCursor..wonder if this is the problem?

Error: 'pFCursor' is passed by reference before it has been assigned a value. A null reference exception could result at runtime.

 Dim pFCursor As IFeatureCursor
                    pFeatureSelection.SelectionSet.Search(Nothing, False, pFCursor)
                    Dim pFeature As IFeature
0 Kudos