3

I would like to edit a selected feature/record via ArcMap vba, but I'm not having much luck with this code sample (see code 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.Selectio nSet.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
PolyGeo
65.5k29 gold badges115 silver badges349 bronze badges
asked Jan 4, 2011 at 12:24

2 Answers 2

5

Try calling Store.

pFeature.value(pFeature.Fields.FindField("Type")) = "update"
pFeature.Store()
Set pFeature = pEnumFeature.Next

Are you in an edit session? Scroll down to best practices: http://help.arcgis.com/en/sdk/10.0/arcobjects_net/conceptualhelp/index.html#/d/00010000010s000000.htm

answered Jan 4, 2011 at 12:50
1

Yes that worked! Here is the updated code.

Private Sub Update_Click()
Dim pFeatureClass As IFeatureClass
Dim pFeatureLayer As IFeatureLayer
Dim pDoc As IMxDocument
Dim pMap As IMap
Set pDoc = ThisDocument
Dim pActiveView As IActiveView
Set pMap = pDoc.ActiveView.FocusMap
Dim pLayer As ILayer
Dim pEditor As IEditor
Dim pEnumFeature As IEnumFeature
Dim pEnumLayer As IEnumLayer
Dim pGeometry As IGeometry
Dim pID As New UID
Dim pFields As IFields
Dim pField As IField
Dim pFeature As IFeature
Dim pFeatureSelection As IFeatureSelection
Set pMap = pDoc.FocusMap
Set pEnumLayer = pMap.Layers
 Set pLayer = pEnumLayer.Next
 Do Until pLayer Is Nothing
 If pLayer.Name = "Copy of Wells_test" Then
 Set pFeatureLayer = pLayer
 End If
 Set pLayer = pEnumLayer.Next
 Loop
Set pFeatureSelection = pFeatureLayer
Set pFeatureClass = pFeatureLayer.FeatureClass
Set pFeatureClass = pFeatureLayer.FeatureClass
Set pFields = pFeatureClass.Fields
Dim strexpression As String
Dim pSelSet As ISelectionSet
 Set pSelSet = pFeatureSelection.SelectionSet
'select feature
 Dim pFCursor As IFeatureCursor
 pSelSet.Search Nothing, False, pFCursor
 Set pFeature = pFCursor.NextFeature
'Loop through the features using the cursor
strexpression = InputBox("Update Type Field")
Do While Not pFeature Is Nothing
pFeature.value(pFeature.Fields.FindField("Type")) = strexpression
pFeature.Store
Set pFeature = pFCursor.NextFeature
Loop
End Sub
George Silva
6,4023 gold badges39 silver badges71 bronze badges
answered Jan 4, 2011 at 13:36
1
  • 2
    Do not post replies as separate answers but make a comment instead. Also do not forget to mark Jay's answer as accepted. Commented Jan 4, 2011 at 14:15

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.