Add a record using MapObjects

To add a record to the attribute table, you need to add a record the the shapefile (i.e. you need to add a shape as well). In order to do that you need to follow the procedure below.Get a reference to the layer

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: , ,

4 Responses to “Add a record using MapObjects”

  1. Leo Says:

    I like this post…
    Now, how do to delete a field in a shapefile using MapObjects?
    Thank you…

  2. iamlaksh1 Says:


    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.

  3. mohd eizaq Says:

    can you give some example n email to me because it not clear for me

  4. iamlaksh1 Says:

    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.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: