Knowledge Base - Technical Articles


Technical Article   HowTo:  Create a sequential unique ID field equivalent to the ArcView 3.x field calculator expression rec+1

Article ID: 27427
Software:  ArcGIS - ArcEditor 8.1, 8.1.2, 8.2, 8.3, 9.0, 9.1, 9.2, 9.3, 9.3.1 ArcGIS - ArcInfo 8.0.1, 8.0.2, 8.1, 8.1.2, 8.2, 8.3, 9.0, 9.1, 9.2, 9.3, 9.3.1 ArcGIS - ArcView 8.1, 8.1.2, 8.2, 8.3, 9.0, 9.1, 9.2, 9.3, 9.3.1
Platforms: N/A

Summary

In ArcView 3.x, typing 'rec+1' in the field calculator creates a sequential unique ID field for a layer. Instructions provided describe how to do the equivalent in the ArcMap field calculator.

Procedure

  1. Add the number field. -show me-

    A. Click Options in the table where the field will be added.
    B. Click Add Field.
    C. Type the name of the field.
    D. Click the Type dropdown arrow and specify the numeric field type.
    E. Set any other field properties.
    F. Click OK.
  2. Click Start Editing on the Editor toolbar.
  3. Right-click the new number field. Click Calculate Values.
  4. Click the Advanced check box.
  5. Copy the following code into the Pre Logic VBA Script code:

    Static rec As Long 
    
    Dim pStart As Long
    Dim pInterval As Long

    ' adjust start value if you want it to
    ' start at a value other than 1.
    ' For example, 1000.
    ' ====================================

    pStart = 1

    ' adjust interval value if you want it to
    ' increment at a value other than 1.
    ' For example, 2.
    ' =======================================

    pInterval = 1

    If (rec = 0) Then
    rec = pStart
    Else
    rec = rec + pInterval
    End If

  6. Type the word 'rec' in the text box under [Your Field Name] =.
  7. Click OK.

Related Information


Created: 8/8/2004
Last Modified: 10/20/2011

Article Rating: (12)
If you would like to post a comment, please login

Comments

By JAB87 - 04/01/2014 1:35 AM

Other - See details below.

This is great, however I need to be able to create a sequential unique ID that follows this pattern: "LV001", "LV002", "LV003" etc. Can anyone advise me on how to tackle this please?

Rating:

By kohyunsung - 12/17/2013 4:49 AM

Other - See details below.

Rating:

By crmeyer - 04/17/2013 3:37 AM

Other - See details below.

reply to Anonymous - 01/05/2010 6:17 AM as work around for sorted columns i suggest to create sequential numbers externally e.g. in excel and paste the numbers from clipboard directly into the first cell. this worked for me.

By yusuph@esri.com - 10/09/2012 12:06 PM

Great article! It helped a lot!

IT WORKS FINE BUT THERE IS NEED FOR MORE ARTICLE ON HOW TO WRITE VBA EXPRESSION FOR BEGINNERS.

Rating:

By Anonymous - 02/25/2011 9:51 AM

The article needs to be updated.

Just what I needed. Worked like a charm, thanks!

Rating:

By Anonymous - 02/24/2011 11:24 AM

The article needs to be updated.

Muchas Gracias, salio a la perfeccion su procedimiento... PURA VIDA

Rating:

By Anonymous - 09/23/2010 7:47 AM

The article needs to be updated.

On further experimentation, at the suggestion of a coworker, I selected my polygons to number sequentially vertically rather than horizontally. Your code works without glitches wiyth this selection. No matter which selection method I use (top to bottom, bottom to top, random)as long as the files are vertically aligned they are numbered from top to bottom. The horizontal selection always went from left to right. Is this because the ArcMap version I am using is set up for American English?

Rating:

By Anonymous - 09/21/2010 3:29 PM

The article needs to be updated.

I selected 35 polygons that I wanted to number sequentially. I entered a new pStart # and left the pInterval at 1. The first 18 numbered correctly but the rest numbered by 2's (31,33,35,etc) and then the inbetween numbers (32,34,36, etc.)were added at the end on the sequence. This is interesting, any ideaas?

Rating:

By Anonymous - 04/06/2010 3:41 PM

The article needs to be updated.

Rating:

By Anonymous - 01/05/2010 6:17 AM

The article is incorrect or the solution didn’t work.

The described solution is not equivalent to the rec+1 approach, because it is not sorting-dependent. I need something that will essentially add the row number of a sorted attribute table, not the unsorted record number. Please indicate a work around.

Rating:

By Anonymous - 04/13/2009 9:57 PM

Great article! It helped a lot!

I need help understanding how the Static variable works and how the VBA script code cursored through each record? A simple for loop doesn't work and I don't understand how declaring the static variable and not setting it to a value made the unique ids. Thanks.

Rating:

By Anonymous - 04/14/2008 7:41 AM

Great article! It helped a lot!

THIS WORKED PERFECTLY.

Rating:

By Anonymous - 11/14/2006 1:09 PM

The article has missing, misleading, or conflicting information.

The field calculator seems to perform its calculations sequentially through the attribute table, regardless of how the table is sorted. So, if you sort by another field, then perform the calculation, the numbers are not sequential. That is the misleading part. Nice use of the static variable to keep track of the counter.

Rating: