2

I've got a shp-file, here is it's attribute table:

Attribute table

It's easy to fill the attribute table of some pFeature programmatically 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

Error message

4

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 IFeatureBuffer with 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
1
  • you are right, it works! Hornbydd, thanks a lot! – Alex Dec 18 '15 at 11:46

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.