Get the recordset for the layer
call the addnew method on the recordset
populate the fields for the new record with data (including the shape field)
call the update method on the recordset to commit the new data
You can not (and should not) add records the attribute table of a shapefile outside of an application (such as MO) that is aware of the linkage between the attribute records and shape records.
To add a field to the attribute table you have a few options. The first is to create a new shapefile based on the old one with the new field added, then to copy all of the data from the old shapefile to the new shapefile using MO methods. This is the safest option, and can be done completely using MO methods. A second choice is to use a non-MO based tool (such as ADO) to add the field the to dBase file. You’ll need to make sure that no map layers refer to the shapefile BEFORE attempting this, but it does save the time/trouble of having to copy the data. Once the new field is added you can query and populate the field using MO methods.
One interesting note is that recordset Fields property returns random order of fields in a collection. If you want to access the fields in their actual physical order, then do not use the Recordset’s Fields property. Rather use the Recordset’s TableDesc Property. The only fields not in the TableDesc are the “Shape” field, and the virtual, internal “FeatureID” field. The only fields which show up in the TableDesc are the fields physically stored in the *.dbf portion of the shapefile.
Another point is that there is no direct method to DELETE any field in a shapefile or feature class using MapObjects.
Tags: MapObjects, Recordset, Tabledesc
July 28, 2008 at 3:43 am
I like this post…
Now, how do to delete a field in a shapefile using MapObjects?
Thank you…
July 30, 2008 at 1:11 pm
Leo:
MapObjects doesnot allow you to delete the fields since TableDesc is Readonly Object. However it is much like oridinary DBF you can modify outside environment or using ArcMap.
November 8, 2008 at 4:57 pm
can you give some example n email to me because it not clear for me
November 8, 2008 at 10:04 pm
Hi Mohd,
Using MO you cannot delete the fields through programmatically, i.e TableDesc object is read-only. If you have further doubts, pl do email me.