Show Navigation | Hide Navigation
You are here:
ArcMap > Working with tables

Making field calculations

Release 9.1
Last modified December 7, 2006
E-mail This Topic Printable Version Give Us Feedback

Print all topics in : "Working with tables"


Related Topics

About making field calculations

Entering values with the keyboard is not the only way you can edit tables. In some cases, you might want to perform a mathematical calculation to set a field value for a single record or even all records. The ArcMap field calculator lets you perform simple as well as advanced calculations on any selected record.

Advanced calculations with VBA statements

The field calculator also lets you perform advanced calculations using VBA statements that process the data before calculations are made on the selected field. For example, using demographic data, you might want to find the largest age group by percentage of the population for each county in the United States. You can create a script that preprocesses your data using logical constructs such as If...Then statements and Select Case blocks. This lets you perform sophisticated calculations quickly and easily.

How to make field calculations


Making simple field calculations

  1. Click Editor on the Editor toolbar and click Start Editing.
  2. You can make calculations without being in an editing session; however, in that case, there is no way to undo the results.
  3. Right-click the layer or table you want to edit and click Open Attribute Table.
  4. Select the records you want to update.
  5. If you don't select any, the calculation will be applied to all records.
  6. Right-click the field heading for which you want to make a calculation and click Calculate Values.
  7. Use the Fields list and Functions to build a calculation expression. You can also edit the expression in the text area.
  8. Optionally, you can type a value to set to the field.
  9. Click OK.
  10. Click Editor on the Editor toolbar and click Stop Editing.


Tips

  • Use double quotes when calculating strings.
  • If you are working with the attribute table of a geodatabase feature class that participates in a geodatabase topology or a geometric network, the Calculate Values command will be disabled when you are not in an edit session.
  • You can't undo a field calculation when performed outside of an edit session.


Making advanced field calculations

  1. Click Editor on the Editor toolbar and click Start Editing.
  2. You can make calculations without being in an editing session; however, in that case, there is no way to undo the results.
  3. Right-click the layer or table you want to edit and click Open Attribute Table.
  4. Select the records you want to update.
  5. If you don't select any, the calculation will be applied to all records.
  6. Right-click the field heading for which you want to make a calculation and click Calculate Values.
  7. Check Advanced.
  8. Type VBA statements in the first text box.
  9. The VBA statements can include ArcMap methods.
  10. Type the variable or value that is to be written to the selected records.
  11. Click OK.


Tips

  • Use double quotes when calculating strings.
  • After entering VBA statements, you can click Save if you want to write them out to a file. The Load button will prompt you to find and select an existing calculation file.
  • For more information on VBA, consult any Visual Basic reference. The Visual Basic Editor—accessed by clicking the Tools menu, pointing to Macros, and clicking Visual Basic Editor—also contains an online reference to Visual Basic commands.
  • If you are working with the attribute table of a geodatabase feature class that participates in a geodatabase topology or a geometric network, the Calculate Values command will be disabled when you are not in an edit session.


Updating area for a shapefile

  1. Click Editor on the Editor toolbar and click Start Editing.
  2. You can make calculations without being in an editing session; however, in that case, there is no way to undo the results.
  3. Right-click the shapefile layer you want to edit and click Open Attribute Table.
  4. Right-click the field heading for area and click Calculate Values.
  5. If there is no field for area values, you can add a new field for area by clicking the Options button and selecting Add Field. However, to add a new field, you need to exit the editing session.
  6. Check Advanced.
  7. Type the following VBA statement in the first text box:

  8. Dim dblArea as double
    Dim pArea as IArea
    Set pArea = [shape]
    dblArea = pArea.area



  9. Type the variable dblArea in the text box directly under the area field name.
  10. Click OK.


Tip

  • The property area returns a field type of double. For best results, your area field should also be a double field type.


Updating perimeter for a shapefile

  1. Click Editor on the Editor toolbar and click Start Editing.
  2. You can make calculations without being in an editing session; however, in that case, there is no way to undo the results.
  3. Right-click the shapefile layer you want to edit and click Open Attribute Table.
  4. Right-click the field heading for area and click Calculate Values.
  5. If there is no field for perimeter values, you can add a new field for perimeter by clicking the Options button and selecting Add Field. However, to add a new field, you need to exit the editing session.
  6. Click Calculate Values.
  7. Check Advanced.
  8. Type the following VBA statement in the first text box:

  9. Dim dblPerimeter as double
    Dim pCurve as ICurve
    Set pCurve = [shape]
    dblPerimeter = pCurve.Length



  10. Type the variable dblPerimeter in the text box directly under the perimeter field name.
  11. Click OK.


Tip

  • The property length returns a field type of double. For best results, your perimeter field should also be a double field type.


Updating length for a shapefile

  1. Click Editor on the Editor toolbar and click Start Editing.
  2. You can make calculations without being in an editing session; however, in that case, there is no way to undo the results.
  3. Right-click the shapefile layer you want to edit and click Open Attribute Table.
  4. Right-click the field heading for length and click Calculate Values.
  5. If there is no field for length values, you can add a new field for length by clicking the Options button and selecting Add Field. However, to add a new field, you need to exit the editing session.
  6. Click Calculate Values.
  7. Check Advanced.
  8. Type the following VBA statement in the first text box:

  9. Dim dblLength as double
    Dim pCurve as ICurve
    Set pCurve = [shape]
    dblLength = pCurve.Length



  10. Type the variable dblLength in the text box directly under the length field name.
  11. Click OK.


Tip

  • The property length returns a field type of double. For best results, your length field should also be a double field type.


Adding the x,y coordinates in a point layer to a new field

  1. Click Editor on the Editor toolbar and click Start Editing.
  2. You can make calculations without being in an editing session; however, in that case, there is no way to undo the results.
  3. Right-click the layer you want to edit and click Open Attribute Table.
  4. Right-click the field heading for the X field and click Calculate Values.
  5. If there is no field for X values, you can add a new field for X by clicking the Options button and selecting Add Field. However, to add a new field, you need to exit the editing session.
  6. Click Calculate Values.
  7. Check Advanced.
  8. Type the following VBA statement in the first text box:

  9. Dim dblX As Double
    Dim pPoint As IPoint
    Set pPoint = [Shape]
    dblX = pPoint.X



  10. Type the variable dblX in the text box directly under the X field name.
  11. Click OK.
  12. Right-click the field heading for the Y field and click Calculate Values.
  13. If there is no field for Y values, you can add a new field for Y by clicking the Options button and selecting Add Field. However, to add a new field you need to exit the editing session.
  14. Click Calculate Values.
  15. Check Advanced.
  16. Type the following VBA statement in the first text box:

  17. Dim dblY As Double
    Dim pPoint As IPoint
    Set pPoint = [Shape]
    dblY = pPoint.Y



  18. Type the variable dblY in the text box directly under the Y field name.
  19. Click OK.


Tip

  • The property for X and Y returns a field type of double. For best results, your X and Y fields should also be a double field type.


Adding the x,y coordinates of the centroid of a polygon layer to a new field

  1. Click Editor on the Editor toolbar and click Start Editing.
  2. You can make calculations without being in an editing session; however, in that case, there is no way to undo the results.
  3. Right-click the layer you want to edit and click Open Attribute Table.
  4. Right-click the field heading for the X field and click Calculate Values.
  5. If there is no field for X values, you can add a new field for X by clicking the Options button and selecting Add Field. However, to add a new field, you need to exit the editing session.
  6. Check Advanced.
  7. Type the following VBA statement in the first text box:

  8. Dim dblX As Double
    Dim pArea As IArea
    Set pArea = [Shape]
    dblX = pArea.Centroid.X



  9. Type the variable dblX in the text box directly under the X field name.
  10. Click OK.
  11. Right-click the field heading for the Y field and click Calculate Values.
  12. If there is no field for Y values, you can add a new field for Y by clicking the Options button and selecting Add Field. However, to add a new field you need to exit the editing session.
  13. Click Calculate Values.
  14. Check Advanced.
  15. Type the following VBA statement in the first text box:

  16. Dim dblY As Double
    Dim pArea As IArea
    Set pArea = [Shape]
    dblY = pArea.Centroid.Y



  17. Type the variable dblY in the text box directly under the Y field name.
  18. Click OK.


Tip

  • The property for X and Y returns a field type of double. For best results, your X and Y fields should also be a double field type.

Please visit the Feedback page to comment or give suggestions on ArcGIS Desktop Help.
Copyright © Environmental Systems Research Institute, Inc.