You are here: > ESRI Forums > arcgis desktop discussion forums > Thread Replies

ArcGIS Desktop Discussion Forums

ArcGIS Desktop - ArcObjects Visual Basic for Application (VBA) forum

How to use an Insert cursor   Glenn Kammerer Sep 18, 2006
Re: How to use an Insert cursor   Miles Hitchen Sep 18, 2006
Re: How to use an Insert cursor   Glenn Kammerer Sep 18, 2006
Re: How to use an Insert cursor   Miles Hitchen Sep 19, 2006
Re: How to use an Insert cursor   Glenn Kammerer Sep 19, 2006
Re: How to use an Insert cursor   Miles Hitchen Sep 19, 2006
Report Inappropriate Content • Top • Print • This Forum is closed for replies.    
Subject How to use an Insert cursor 
Author Glenn Kammerer 
Date Sep 18, 2006 
Message ArcMap 9.1
Windows Xp Pro

I am trying to build a new table based on an existing table. The existing table is actually the attribute table for a road centerline featureclass. What I'm wanting to do is to take the existing road centerline featureclass, sort it, then loop through the tablesort object and write those rows to a new table. So far I've been able to create the tablesort object and I've been able to create a new table with exactly the same fields as the original featureclass (and thus the tablesort object as well), but I'm stuck on how to write the rows from the tablesort object to the new table. I realize I'll need an insert cursor at some point, but I don't know the best way to do this. Could somebody give me a few suggestions on where to start or what interfaces to look at? Can what I want even be done? 
  <><><><><><><><><><><><><><><>

Glenn Kammerer
GIS Analyst
Spatial Data Research, Inc.

<><><><><><><><><><><><><><><> 
   
Report Inappropriate Content • Top • Print • This Forum is closed for replies.    
Subject Re: How to use an Insert cursor 
Author Miles Hitchen 
Date Sep 18, 2006 
Message Hi Glenn,

The code snippet below should get you started, you need to look at IFeatureClass, IFeatureBuffer and IFeatureCursor. The snippet assumes that pFtrCls has already been set to the output featureclass.

Hope that helps,
Miles
 
 
Dim pFtrCsr As IFeatureCursor
Dim pFtrBfr As IFeatureBuffer

    ' Create a feature buffer, used to temporarily store
    ' feature data before it is inserted into a featureclass
    Set pFtrBfr = pFtrCls.CreateFeatureBuffer
    
    ' Create an Insert FeatureCursor, specifying
    'that we'll be using a FeatureBuffer
    Set pFtrCsr = pFtrCls.Insert(True)
    
    ' Add some values to the featurebuffers fields
    ' This featurebuffer acts just like a feature
    ' but we can only write to it, not read from it
    pFtrBfr.Value(2) = vSomeValue       ' a value for the 3rd field
    Set pFtrBfr.Shape = pSomeGeometry   ' the new geometry for the feature
    
    ' Insert this into the featureclass using the featurecursor
    pFtrCsr.InsertFeature pFtrBfr
    
    ' We can now repopulate the fields of the feature buffer and
    ' continue inserting as above
 
  Miles Hitchen
Software Engineer
Geospatial Team
Ordnance Survey
UK
 
   
Report Inappropriate Content • Top • Print • This Forum is closed for replies.    
Subject Re: How to use an Insert cursor 
Author Glenn Kammerer 
Date Sep 18, 2006 
Message Thanks for the help, Miles.

Below is a sample of my code. Cursors and layers and such are dimensioned and set earlier in the code.

Problem now is I'm getting a run time error Field is not editable.

Any thoughts? 
 
Dim strPath As String
    Dim strFolder As String

    Set pFields = pFClass.Fields
    strPath = GetFLayerPath(pFlayer)   'custom function
    strFolder = GetFolderString(strPath)  'custom function
    Set pQTable = createDBF(pFlayer.Name & "_QAQC", strFolder, pFields) 'function from sample code
    
    Dim pQFClass As IFeatureClass
    Set pQFClass = pQTable
    
    Set pFBuffer = pQFClass.CreateFeatureBuffer
    Set pFCursor = pQFClass.Insert(True)
    
    Dim pPLine As IPolyline

    Set pCursor = pSTable.Rows      'pSTable previously created = ITableSort object
    Set pRow = pCursor.NextRow
    Do While Not pRow Is Nothing
      For i = 0 To pFields.FieldCount - 1
         pFBuffer.Value(i) = pRow.Value(i)      '<===  error here
         Set pPLine = New Polyline
         Set pFBuffer.Shape = pPLine
         pFCursor.InsertFeature pFBuffer
      Next i
      Set pRow = pCursor.NextRow
    Loop
    pFCursor.Flush
 
  <><><><><><><><><><><><><><><>

Glenn Kammerer
GIS Analyst
Spatial Data Research, Inc.

<><><><><><><><><><><><><><><> 
   
Report Inappropriate Content • Top • Print • This Forum is closed for replies.    
Subject Re: How to use an Insert cursor 
Author Miles Hitchen 
Date Sep 19, 2006 
Message Hi Glen,

The problem is that you're code is trying to copy the old OID to the new record (which can't be done as this is a non-editable field and it's value is automatically generated by Arc). You need to put in a check to see if the field is editable, if it's not then don't assign a value to it, you can use IField.Editable for this.

Also, I noticed a couple of problems with your code, you're creating an empty polyline to store in the featureclass, although this may work, you wont be able to see anything in your map! The other thing is that you're assigning this geometry inside the fields loop, it only needs to be done once, outside the loop.

hope that helps,
Miles.

 
  Miles Hitchen
Software Engineer
Geospatial Team
Ordnance Survey
UK
 
   
Report Inappropriate Content • Top • Print • This Forum is closed for replies.    
Subject Re: How to use an Insert cursor 
Author Glenn Kammerer 
Date Sep 19, 2006 
Message Miles,

Thanks for your help. I need to reevaluate what I want to do. I don't actually need any geometry data in my output table. In a nutshell, I want to take the table of a featureclass, sort it, and then re-create the sorted table in a new standalone table. No spatial or geomterical requirements whatsoever, I strictly want to work with attribution that's permanently sorted. I may be posting a new topic shortly. Thanks again for the help, I'll be saving this bit of code for another day. :) 
  <><><><><><><><><><><><><><><>

Glenn Kammerer
GIS Analyst
Spatial Data Research, Inc.

<><><><><><><><><><><><><><><> 
   
Report Inappropriate Content • Top • Print • This Forum is closed for replies.    
Subject Re: How to use an Insert cursor 
Author Miles Hitchen 
Date Sep 19, 2006 
Message Hi Glenn,

OK, I see now! You're code will work, all you need to do is:

1. Make sure you're output table is a .dbf file (it will still be an ITable in ArcObjects) and doesn't contain a "Shape" field.

2. When you output the fields to the new table make sure you don't output the OID field or the Shape field. These can easily be checked via the IFeatureClass.OIDFieldName and IFeatureClass.ShapeFieldName.

If this isn't clear enough then post a link to your new thread and I'll take a look at it.

Cheers,
Miles. 
  Miles Hitchen
Software Engineer
Geospatial Team
Ordnance Survey
UK