Auto populate sequential unique id's

05-20-2011 02:46 PM
Occasional Contributor
Does anyone know how to automatically sequentially calculate numbers in an ID field. I have a VBA script that updates fields with a click but i would like to auto populate a field with sequential number. The field i need auto populate when i click is Address_ID. I need it to some how look at the last number and determine which would be next and populate it. I need to some how incorporate into my VBA script i have. Any help would be gratefully appreciated.

0 Kudos
2 Replies
Occasional Contributor
It can't be impossible right?

1. sort the table by field Address_ID.
2. Generate ID by adding 1 to the last Address_ID

0 Kudos
Occasional Contributor
Ok i found this Automated unique sequential ID field script that works great for creating sequential unique id's but i need it to generate on a click event. I need it to generate the unique sequential Id when i click on a button the button that populates certain fields.

Another issue is that i need it to update a string filed (Address_ID; CC1234) not just a numeric field.

Here is the code i found and working with.

Option Explicit

Private m_bPopulate As Boolean
Private m_EditEventChange As Boolean
Private WithEvents m_pEditEvents As Editor

Private Function MxDocument_OpenDocument() As Boolean

Dim pEditor As IEditor
Dim pUID As New UID

pUID = "esriCore.Editor"
Set pEditor = Application.FindExtensionByCLSID(pUID)

If pEditor Is Nothing Then
MsgBox "Unable to Enable UIC Custom Editing Environment", vbOKOnly, "ERROR...UIC Editing Environment"
Exit Function
End If

Set m_pEditEvents = pEditor
m_bPopulate = True
m_EditEventChange = False

MsgBox "Edit Event Field Population is Enabled", vbOKOnly, "AGRC Custom Editing..."

End Function

Private Sub m_pEditEvents_OnCreateFeature(ByVal obj As esriGeoDatabase.IObject)

'Creates new ID number for new feature

Dim pRow As IRow
Dim newIDIndex As Integer
Dim pTable As ITable
Dim pCursor As ICursor
Dim pData As IDataStatistics
Dim pStatResults As IStatisticsResults
Dim IDMax As Variant
Dim NewID As Long
Dim changeMade As Boolean

Set pRow = obj

If Not m_bPopulate Then Exit Sub

'Find index (as long) of "ID_NUMBER" field
newIDIndex = pRow.Fields.FindField("AddresID_2")

'Set up cursor
Set pTable = obj.Table
Set pCursor = pTable.Search(Nothing, False)

'Set up statistics parameters
Set pData = New DataStatistics
pData.Field = "AddresID_2"
Set pData.Cursor = pCursor

'Find the max value of the "ID_NUMBER" field
Set pStatResults = pData.Statistics
IDMax = pStatResults.Maximum

'Add 1 to create next (sequential)ID number
NewID = IDMax + 1

changeMade = False

If newIDIndex > 0 Then
pRow.Value(newIDIndex) = NewID
changeMade = True
End If

If changeMade Then
m_EditEventChange = True
End If

m_EditEventChange = False

End Sub
0 Kudos