HOW TO

Copy or load data and preserve GlobalID values with VBA

Last Published: April 25, 2020

Summary

Instructions provided describe how to copy or load data and preserve GlobalID values with VBA. There are two approaches that can be used to do this using ArcObjects. The SDK article, How to copy or load data and preserve GlobalID values, has a more detailed discussion on this and includes .NET code. This article includes VBA versions of the code in that article.

Procedure

The VBA code below is an implementation of the two approaches described in the article referenced above. This code can be run from VBA in ArcCatalog. The code works on the selected feature class or table in ArcCatalog.
Code:
' Code for Approach 1

' converts a Globalid to a GUID
Sub GlobalID_to_GUID()

' Get the table
Dim pGxApp As IGxApplication
Set pGxApp = Application
Dim pGxObj As IGxObject
Set pGxObj = pGxApp.SelectedObject
Dim pName As IName
Set pName = pGxObj.InternalObjectName
Dim pTable As ITable
Set pTable = pName.Open

' Get the globalid field
Dim classEX As IClassEx
Dim pDSet As IDataset
Set classEX = pTable
If (Not classEX.HasGlobalID) Then
Set pDSet = pTable
MsgBox "No globalid field exists for table: " & pDSet.Name
Exit Sub
End If

' Convert the GlobalID column to a GUID column
Dim schemaEdit As IClassSchemaEditEx
Set schemaEdit = pTable
schemaEdit.UnregisterGlobalIDColumn classEX.GlobalIDFieldName

End Sub

' converts a GUID to a GlobalID
Sub GUID_to_GlobalID()

' Get the table
Dim pGxApp As IGxApplication
Set pGxApp = Application
Dim pGxObj As IGxObject
Set pGxObj = pGxApp.SelectedObject
Dim pName As IName
Set pName = pGxObj.InternalObjectName
Dim pTable As ITable
Set pTable = pName.Open

' Get the GUID field to convert
Dim fields As IFields
Dim pDSet As IDataset
Dim i As Integer
Set fields = pTable.fields
i = fields.FindField("globalid")
If (i = -1) Then
Set pDSet = pTable
MsgBox "A field named globalid does not exist in table: " & pDSet.Name & _
". If the field that you want to convert has another name, adjust the code and re-run it."
Exit Sub
End If

' make sure it is of type GUID
Dim GUIDField As IField
Set GUIDField = fields.Field(i)
If (GUIDField.Type <> esriFieldTypeGUID) Then
MsgBox "Field " & GUIDField.Name & " is not of type GUID"
Exit Sub
End If

' Convert the GUID column to a GlobalID column
Dim schemaEdit As IClassSchemaEditEx
Set schemaEdit = pTable
schemaEdit.RegisterGlobalIDColumn GUIDField.Name

End Sub



Code:
' Code for Approach 2

' Export the table to updategram
Sub Export_table_to_updategram()

' Get the table
Dim pGxApp As IGxApplication
Set pGxApp = Application
Dim pGxObj As IGxObject
Set pGxObj = pGxApp.SelectedObject
Dim pName As IName
Set pName = pGxObj.InternalObjectName
Dim pTable As ITable
Set pTable = pName.Open

' unqualify the table name
Dim pDSet As IDataset
Dim unqualTableName As String
Set pDSet = pTable
unqualTableName = pDSet.Name
unqualTableName = Right(unqualTableName, Len(unqualTableName) - InStr(unqualTableName, "."))

' initialize the TablesDataChanges with inserts only
Set pDSet = pTable
Dim tableDataChangesInfo As ITableDataChangesInfo
Set tableDataChangesInfo = New tableDataChangesInfo
tableDataChangesInfo.Init unqualTableName, pTable, Nothing, Nothing
Dim tablesDataChanges As ITablesDataChanges
Set tablesDataChanges = New tablesDataChanges
tablesDataChanges.Init esriReplicaModelType.esriModelTypeFullGeodatabase
tablesDataChanges.Add tableDataChangesInfo
Dim dataChanges As IDataChanges
Set dataChanges = tablesDataChanges

' Use the TablesDataChanges to generate an updategram
Dim path As String
path = Environ("temp")
Dim detlaFile As String
detlaFile = path & "\updategram_of_table_" & unqualTableName & ".xml"
Dim DataChangesExporter As IExportDataChanges
Set DataChangesExporter = New DataChangesExporter
DataChangesExporter.ExportDataChanges detlaFile, esriExportDataChangesOption.esriExportToXML, dataChanges, True

InputBox "Table or feature class " & pDSet.Name & " has been export to the following updategram:", "Export to delta file", detlaFile

End Sub

' Imports from a delta file
' NOTE: you will need to change the following path in the code "d:\temp\expver.xml"
Sub impDataChanges()

' Get the ArcSDE workspace
Dim pGxApp As IGxApplication
Set pGxApp = Application
Dim pGxObj As IGxObject
Set pGxObj = pGxApp.SelectedObject
Dim pName As IName
Set pName = pGxObj.InternalObjectName
Dim pWkSpname As IWorkspaceName
Set pWkSpname = pName

Dim pDelChgs As IDeltaDataChangesInit
Set pDelChgs = New DeltaDataChanges
pDelChgs.Init "d:\temp\expver.xml", esriExportToXML

Dim pImpDC As IImportDataChanges
Set pImpDC = New DataChangesImporter
pImpDC.ImportDataChanges pWkSpname, pDelChgs, False, False

End Sub

    Article ID:000010306

    Software:
    • ArcMap 9 x

    Get help from ArcGIS experts

    Contact technical support

    Download the Esri Support App

    Go to download options

    Discover more on this topic