How to copy attribute domains


A domain specifies the permissible values that a geodatabase field may take. This example copies all the domains from one geodatabase to another. Note that each domain object is cloned before being added to the destination geodatabase.

How to use

  1. Copy the two macros into your VB or VBA application.
  2. Edit the Main macro to point to your data.
  3. Run the Main macro.
[VBA]
Public Function CopyDomains(pOrigWorkspace As IWorkspace, pDestWorkspace As IWorkspace) As Integer
    
    Dim pOrigWSDomains As IWorkspaceDomains
    Dim pDestWSDomains As IWorkspaceDomains
    Set pDestWSDomains = pDestWorkspace
    Set pOrigWSDomains = pOrigWorkspace
    
    Dim pEnumDomain As IEnumDomain
    Dim pDomain As IDomain
    Dim iAdded As Integer
    iAdded = 0
    
    Set pEnumDomain = pOrigWSDomains.Domains
    If Not pEnumDomain Is Nothing Then
        pEnumDomain.Reset
        Set pDomain = pEnumDomain.Next
        Do Until pDomain Is Nothing
            Dim pClone As IClone
            Set pClone = pDomain
            Set pDomain = pClone.Clone
            pDestWSDomains.AddDomain pDomain
            iAdded = iAdded + 1
            Set pDomain = pEnumDomain.Next
        Loop
    End If
    
    CopyDomains = iAdded
End Function

Public Sub Main()
    Dim pOrigFeatWorkspace As IFeatureWorkspace
    Dim pDestFeatWorkspace As IFeatureWorkspace
    Dim pWSF As IWorkspaceFactory
    Set pWSF = New AccessWorkspaceFactory
    'Open the origin and destination workspaces. *** Modify the pathnames appropriately.
    Set pOrigFeatWorkspace = pWSF.OpenFromFile("D:\Data\Geodatabases\OriginGDB.mdb", 0)
    Set pDestFeatWorkspace = pWSF.OpenFromFile("D:\Data\Geodatabases\TargetGDB.mdb", 0)
    
    Dim iAdded As Integer
    iAdded = CopyDomains(pOrigFeatWorkspace, pDestFeatWorkspace)
    MsgBox CStr(iAdded) & " domains copied"
End Sub






Additional Requirements
  • Two Geodatabases, with the origin Geodatabase containing attribute domains.