Can't fill the attribute table programmatically
I've got a shp-file, here is it's attribute table:
It's easy to fill the attribute table of some
pFeatureprogrammatically if I get the feature from cursor:
For example, "RECNO" Field(3) has a numeric format
Set pFeature = pFeatureCursor.NextFeature Set pFeature.Value(3) =2 pFeature.store
Usually, it works fine, I shouldn't even start the Edit session for this.
But when I add a new feature and try to fill the attribute table in this code below, the error "Object required" appears.
Dim pMxDoc As IMxDocument Dim pFeatureLayer As IFeatureLayer Dim pFeature As IFeature Dim pFeatureClass1 As IFeatureClass Set pMxDoc = ThisDocument Set pFeatureLayer = pMxDoc.FocusMap.Layer(0) Set pFeatureClass1 = pFeatureLayer.FeatureClass Set pFeature = pFeatureClass1.CreateFeature ' HERE IS THE PROBLEM Set pFeature.Value(3) = 2
I believe the code you have:
Set pFeature.Value(3) = 2
needs to be
pFeature.Value(3) = 2
The reason is that the value it is storing is a number and not an object.
If you are planning to run this in a loop, consider using an
IFeatureBufferwith an insert cursor, I believe these are faster. Below is some example code from an old project of mine.
For i = 0 To aDict.Count - 1 Let aKey = thekeys(i) ' aKey is dist from route mouth Set pPoint = aDict.Item(aKey) ' point Set pFeatureBuffer = pFeatureClass_Output.CreateFeatureBuffer Set pFeatureBuffer.Shape = pPoint With pFeatureBuffer .Value(.Fields.FindField("ID")) = spID .Value(.Fields.FindField("Dist2Mouth")) = aKey .Value(.Fields.FindField("PolylineID")) = pFeature_WB.OID .Value(.Fields.FindField("WBID")) = WBID End With Let spID = spID + 1 pFeatureCursor_SP.InsertFeature pFeatureBuffer Next i