VBA Edit & Timestamp Script PDF Print E-mail

Written by Bert Granberg,

These three functions (code attached below) can be used to auto-populate 2 'timestamp' fields and an 'edited by' field within an ArcMap edit session whenever a feature is created or changed. They should work with all geodatabases (SDE, file, personal) and shapefiles.

Paste these functions into MXD's Project-->ArcMap Objects-->ThisDocument code window.

  1. From the ArcMap menu, navigate to Tools --> Macros --> Visual Basic Editor to open the VBA editor
  2. In the VBA Editor's Project Window, expand the Project --> Arcmap Objects folder and double click on ThisDocument. Alternatively, paste into your Normal --> ArcmapObjects --> ThisDocument (the Normal template) code window if you want this functionlity in all projects on your machine.
  3. Paste the code (below) in the window that opens and close the VBA editor.
  4. Then save, exit and reopen the MXD. This is important because you need to get an OpenDocument event to fire to initialize the code

As posted below the code will work with the field name and types listed below. Field names are case sensitive and can, of course, be customized in the code. Missing fields will be ignored.

Create Feature Event - Impacted Field Names (Type) --> Set to Value:

  • EditedBy (Text) -->Windows User Name
  • CreatedOn (Date) --> Now Date/Time
  • ModifiedOn (Date) --> Now Date/Time
  • AGRC_MDATE (Date)  --> Now Date/Time

Edit Feature Event - Impacted Field Names (Type) --> Set to Value:

  • EditedBy (Text) -->Windows User Name
  • ModifiedOn (Date) --> Now Date/Time
  • AGRC_MDATE (Date)  --> Now Date/Time

Note: If this code or any other code is interupted or errors out, you'll need to rerun the MxDocument_OpenDocument function to get the edit stamping to start working again.


Function Descriptions: 

MxDocument_OpenDocument -  This script must be run before the edit event scripts will be triggered. It will run each time an ArcMap project with this function in it is opened or the script ican be run manually. To get these edit stamping scripts to work after initially installing them, you'll have to either 1) save your project and reopen it, or 2) manually run this script. This script gets a handle on the ArcMap Editor object and initializes the process to listen for feature creation and feature edit events.

m_pEditEvents_OnCreateFeature - This script is triggered by the creation of a new feature using the ArcMap interface within an edit session. It populates the EditedBy field with the current user's windows login name and the CreatedOn and ModifiedOn date with the current system time.

m_pEditEvents_OnChangeFeature - This script is triggered by an attribute or geometry edit to a feature using the ArcMap interface within an edit session. It populates the EditedBy field with the current user's windows login name and ModifiedOn date with the current system time.


VBA Code:

'3/13/08 This e-mail address is being protected from spam bots, you need JavaScript enabled to view it  

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_OnChangeFeature(ByVal obj As esriGeoDatabase.IObject)
    If m_EditEventChange = False Then

          Dim pRow As IRow
          Set pRow = obj
        
          
          If Not m_bPopulate Then Exit Sub
          
          Dim editedByFieldIndex As Integer
          Dim modifiedOnFieldIndex As Integer
          Dim mDateFieldIndex As Integer
          Dim changeMade As Boolean
          editedByFieldIndex = pRow.Fields.FindField("EditedBy")
          modifiedOnFieldIndex = pRow.Fields.FindField("ModifiedOn")
          mDateFieldIndex = pRow.Fields.FindField("AGRC_MDATE")
          
          changeMade = False
          
          If editedByFieldIndex > 0 Then
            pRow.Value(editedByFieldIndex) = Environ("USERNAME")
            changeMade = True
          End If
          
          If modifiedOnFieldIndex > 0 Then
            pRow.Value(modifiedOnFieldIndex) = Now
            changeMade = True
          End If
          
          If mDateFieldIndex > 0 Then
            pRow.Value(mDateFieldIndex) = Now
            changeMade = True
          End If
          
          If changeMade Then
            m_EditEventChange = True
            pRow.Store
          End If
    End If
    m_EditEventChange = False
End Sub

Private Sub m_pEditEvents_OnCreateFeature(ByVal obj As esriGeoDatabase.IObject)
          'MsgBox Now
            
  Dim pRow As IRow
  Dim CreatedOnFieldIndex As Integer
  Dim modifiedOnFieldIndex As Integer
  Dim editedByFieldIndex As Integer
  Dim mDateFieldIndex As Integer
  Dim changeMade As Boolean
 
  Set pRow = obj
 
  If Not m_bPopulate Then Exit Sub
 
  CreatedOnFieldIndex = pRow.Fields.FindField("CreatedOn")
  modifiedOnFieldIndex = pRow.Fields.FindField("ModifiedOn")
  editedByFieldIndex = pRow.Fields.FindField("EditedBy")
  mDateFieldIndex = pRow.Fields.FindField("AGRC_MDATE")
 

  changeMade = False
 
  If modifiedOnFieldIndex > 0 Then
    pRow.Value(modifiedOnFieldIndex) = Now
    changeMade = True
  End If
 
  If mDateFieldIndex > 0 Then
    pRow.Value(mDateFieldIndex) = Now
    changeMade = True
  End If
 
  If CreatedOnFieldIndex > 0 Then
    pRow.Value(CreatedOnFieldIndex) = Now
    changeMade = True
  End If
 
  If editedByFieldIndex > 0 Then
    pRow.Value(editedByFieldIndex) = Environ("USERNAME")
    changeMade = True
  End If
 
  If changeMade Then
    m_EditEventChange = True
    pRow.Store
  End If
  m_EditEventChange = False
End Sub


Users' Comments  
 

Display 5 of 5 comments

1. Sun, 03-23-2008 at 08:19 PM

Great script! This is a nice, customizable alternative for those of us who don't want to delve into the timestamper class extension. Thanks!

2. Thu, 12-04-2008 at 08:41 AM

Thanks for the script! It's works great and will save me plenty of clicks. One question. Instead of the "ModifiedOn" and "CreatedOn" values showing date and time I would like to see just the Date. Instead of "now" what would you use to get this result? 
 
Thanks again!

3. Thu, 12-04-2008 at 08:46 AM

Never mind! I figured it out. Instead of "now" I entered "Date" and it works perfect.

4. Tue, 05-12-2009 at 02:45 PM

I have been loving this tool. I found a problem today though. I have created a geometric network and it seems the tool doesn't work anymore. It starts up fine when I open my MXD but get an error when I perform the first edit. The debugger points to pRow.Store. Any suggestions would be greatly appreciated!

5. Tue, 06-02-2009 at 04:13 PM

Hi Bert, 
 
Thanks for the script it works really well. One question for you: should it work when opening a blank new mxd? It works well with existing mxds but not blank new. I need to first add some data, save and reopen. The code is stored in the normal. mxt. 
 
Thanks 
 
Kat

Display 5 of 5 comments

Add your comment

13, Mar. 2008
Last Updated ( 20, Nov. 2008 )
 
< Prev   Next >

AGRC Contacts | UGIC Contacts

feed image feed image

Utah GIS Portal © 2009 AGRC

Optimized for