Calculate Field (with Join)

Calculates a field within a Feature Layer or Table View to a given expression. The output is written to a new Feature Class or Table. (This tool was written because the CalculateField field method does not allow you to edit a table that has a Join.)

Usage Tips

When the input is a Feature Layer, it can reference either a shapefile or geodatabase. When the input is a Table View, it can reference either a dBase table or a geodatabase table.

The output dataset must be of the same data type as the dataset referenced by the input Feature Layer or Table View.

This tool is susceptible to failure due to a SCHEMA LOCK, especially when the datasets are geodatabases. If the datasets are geodatabases, the tool will fail if a schema lock is in place on the database. First remove all layers from ArcMap that reference the database. Next delete the schema lock file if it still exists. In the case of a personal geodatabase the schema lock file bears the same name as the database, but has a .ldb extension. If the schema lock file can not be deleted, exit ArcMap. Again try to delete it. (You may have to wait up to a minute before the file can be deleted.) If all else fails, reboot the computer, and try to delete the schema lock file.

Command line syntax

CalculateField_Join <Layer_File> <Field_Name> <Expression> <Output_Feature_Class>

Parameters

Expression Explanation
<Layer_File>

The input Feature Layer or Table View.

When the input is a Feature Layer, it can reference either a shapefile or geodatabase. When the input is a Table View, it can reference either a dBase table or a geodatabase table.

If the Feature Layer or Table View references a geodatabase, then all layers that reference this geodatabase must be removed from ArcMap before this tool can be run.

<Field_Name>

The field in the input table whose values will be calculated using the Expression.

<Expression>

The expression used to calculate new values for the field specified by the Field Name.

The same rules apply as do for the Expression used by the CalculateField tool.

As as, when field names are referenced in the Expression, they must be enclosed in square brackets (e.g. [Shape_Area]).

<Output_Feature_Class>

The name for the output feature class or table.

The output dataset must be of the same data type as the dataset referenced by the input Feature Layer or Table View. E.g. if the input is a Table View that references a dBase table, then the output dataset must be a dBase table.

When the output dataset is a shapefile or a dBase table, it is not necessary to enter the '.shp' or '.dbf' extensions.

The output dataset does not retain the join(s) that the input Feature Layer or Table View had. (And the field names revert back to their original names.)

The output dataset must not already exist.

Command Line Example

workspace c:\workspace\facts
Set c:\workspace\facts into workspace

CalculateField_Join activity_region.lyr activity_region.polyno [activity_poly_rxp.polyno] activity_region2.shp
Executing (CalculateField_Join_1): CalculateField_Join "C:\workspace\facts\activity_region.lyr" activity_region.polyno [activity_poly_rxp.polyno] activity_region2.shp
Start Time: Sun May 28 18:16:45 2006
Running script CalculateField_Join...
Completed script CalculateField_Join...
Executed (CalculateField_Join_1) successfully.
End Time: Sun May 28 18:17:13 2006 (Elapsed Time: 28.00 secs)

Scripting syntax

CalculateField_Join (Layer_File, Field_Name, Expression, Output_Feature_Class)

Parameters

Expression Explanation
Layer File (Required)

The input Feature Layer or Table View.

When the input is a Feature Layer, it can reference either a shapefile or geodatabase. When the input is a Table View, it can reference either a dBase table or a geodatabase table.

If the Feature Layer or Table View references a geodatabase, then all layers that reference this geodatabase must be removed from ArcMap before this tool can be run.

Field Name (Required)

The field in the input table whose values will be calculated using the Expression.

Expression (Required)

The expression used to calculate new values for the field specified by the Field Name.

The same rules apply as do for the Expression used by the CalculateField tool.

As as, when field names are referenced in the Expression, they must be enclosed in square brackets (e.g. [Shape_Area]).

Output Feature Class (Required)

The name for the output feature class or table.

The output dataset must be of the same data type as the dataset referenced by the input Feature Layer or Table View. E.g. if the input is a Table View that references a dBase table, then the output dataset must be a dBase table.

When the output dataset is a shapefile or a dBase table, it is not necessary to enter the '.shp' or '.dbf' extensions.

The output dataset does not retain the join(s) that the input Feature Layer or Table View had. (And the field names revert back to their original names.)

The output dataset must not already exist.

Script Example

# Import modules.
import sys, string, os, win32com.client

# Create the Geoprocessor object
gp = win32com.client.Dispatch('esriGeoprocessing.GpDispatch.1')

# Load DV Region toolbox.
gp.AddToolbox('C:/Documents and Settings/dchatfie/Application Data/ESRI/ArcToolbox/My Toolboxes/' + \
                         'DV Data Management Tools.tbx')

# Set workspace.
gp.workspace = 'c:/workspace/facts'

# Add polyno field to input region feature class
gp.addfield('activity_region.shp', 'polyno', 'long', 9, 0, 9)

# Join input region feature class with RXP table.
gp.makefeaturelayer('activity_region.shp', 'lyr')
gp.addjoin('lyr', 'objectid', 'activity_poly_rxp.dbf', 'subclassno')

# Calculate polyno field using Join to RXP table.
gp.calculatefield_join('lyr', 'activity_region.polyno', '[activity_poly_rxp.polyno]', 'activity_region2.shp')

# Remove Join.
gp.removejoin('lyr', 'activity_poly_rxp')