|
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. - From the ArcMap menu, navigate to Tools --> Macros --> Visual Basic Editor to open the VBA editor
- 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.
- Paste the code (below) in the window that opens and close the VBA editor.
- 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 |