how to modify VBA as python

818
3
Jump to solution
04-27-2012 12:39 PM
ElaineKuo
Occasional Contributor
Hello,

I created a VBA to generate a new field (A_new)(season) of polygon and then copy an existing field (A_old) to it.
(For ArcInfo 9.3)

However, there are 500 shapefiles waiting for this task.
The file names are different but share the beginning of C. (For instance, C9568, C4803, and C3208)
Please kindly advise if there is any VBA code to copy the existing fields (C9568, C4803, and C3208)
to the new field with the same name (season) without doing it for 500 times.
Thanks a lot.


Code

Public Sub CreateField()
On Error GoTo eh:

Dim pMxDoc As IMxDocument
Set pMxDoc = ThisDocument
Dim pMap As IMap
Set pMap = pMxDoc.FocusMap
Dim pLayer As ILayer
Set pLayer = pMap.Layer(0)
Dim pFeatureLayer As IFeatureLayer
Dim pFeatureClass As IFeatureClass
If TypeOf pLayer Is IFeatureLayer Then
Set pFeatureLayer = pLayer
Else
Exit Sub
End If
Set pFeatureClass = pFeatureLayer.FeatureClass
Dim pField As IField
Dim pFieldedit As IFieldEdit
Set pField = New Field
Set pFieldedit = pField
With pFieldedit
.Name = "Season"
.AliasName = "Season"
.Type = esriFieldTypeString
.Length = 6
End With
pFeatureClass.AddField pField
Call UpdateFeatures
eh:
End Sub

Public Sub UpdateFeatures()
Dim pFeatureClass As IFeatureClass
Dim pFeatureLayer As IFeatureLayer
Dim pDoc As IMxDocument
Dim pMap As IMap

Set pDoc = ThisDocument
Set pMap = pDoc.Maps.Item(0)
Set pFeatureLayer = pMap.Layer(0)
Set pFeatureClass = pFeatureLayer.FeatureClass

Dim pFeatureCursor As IFeatureCursor
Dim pFeature As IFeature

Set pFeatureCursor = pFeatureClass.Update(Nothing, False)

'++++++ Loop through each feature and update
Set pFeature = pFeatureCursor.NextFeature
Do While Not pFeature Is Nothing
pFeature.Value(pFeature.Fields.FindField("Season")) = pFeature.Value(pFeature.Fields.FindField("C9469"))
pFeature.Store
Set pFeature = pFeatureCursor.NextFeature
Loop
End Sub
Tags (2)
0 Kudos
1 Solution

Accepted Solutions
DarrenWiens2
MVP Honored Contributor
In Python:
- List feature classes
- Loop through each feature class in the list
- Add a field
- Calculate the field

View solution in original post

0 Kudos
3 Replies
DarrenWiens2
MVP Honored Contributor
In Python:
- List feature classes
- Loop through each feature class in the list
- Add a field
- Calculate the field
0 Kudos
ElaineKuo
Occasional Contributor
Thanks a lot.
It saves a lot of time
0 Kudos
TonyAlmeida
Occasional Contributor II
Helenak,

Could you post your code please.
I would like to see how you accomplished converting your vba code to python.
I am working on the same thing.

Thanks.
0 Kudos