How to assign attribute domains to an object class' subtypes


Attribute domains are a set of coded values or range of values that can be stored in a field. Domains are created and stored in the workspace, but used with a table, feature class or subtype. To create an attribute rule, you must associate a domain with a field from an object class or subtype. The following example shows how you create a domain in a geodatabase and associate it a field for a particular subtype.

How to use

  1. Change the path in the VBA code to point to the USA geodatabase in the developer sample directory.
  2. Paste the code into your VBA Application and run "Assign_Domain".
[VBA]
Sub Assign_Domain()
    On Error GoTo ErrorHandler
    
    'Open the workspace
    Dim pFact As IWorkspaceFactory
    Set pFact = New AccessWorkspaceFactory
    
    Dim pWorkspace As IWorkspace
    'Change this path to point to the USA geodatabase in the developer sample directory
    Set pWorkspace = pFact.OpenFromFile("C:\Program Files\ArcGIS\DeveloperKit10.0\Samples\data\usa\usa.mdb", 0)
    
    Dim pFeatws As IFeatureWorkspace
    Set pFeatws = pWorkspace
    
    ' QI for the IWorkspaceDomains interface
    Dim pWorkspaceDomains2 As IWorkspaceDomains
    Set pWorkspaceDomains2 = pWorkspace
    
    ' get the coded value domain
    Dim pDomain As IDomain
    Set pDomain = GetDomain(pWorkspaceDomains2)
    
    'Get the States FeatureClass
    Dim pFeatcls As IFeatureClass
    Set pFeatcls = pFeatws.OpenFeatureClass("States")
    
    'QI for the ISubtypes interface
    Dim pSubType As ISubtypes
    Dim lSubT As Long
    Set pSubType = pFeatcls
    
    Set pSubType.Domain(pSubType.DefaultSubtypeCode, "SUB_REGION") = pDomain
    
    MsgBox "Done"
    Exit Sub
ErrorHandler:
    MsgBox Err.Description
End Sub

Private Function GetDomain(ByVal pWorkspaceDomains2 As IWorkspaceDomains2) As IDomain
    'Check to see if the domain already exists
    Dim pDomain As IDomain
    Set pDomain = pWorkspaceDomains2.DomainByName("SubRegionDomain")
    If Not pDomain Is Nothing Then
        Set GetDomain = pDomain
        Exit Function
    End If
    
    'Create a new domain in memory
    Set pDomain = New CodedValueDomain
    pDomain.Name = "SubRegionDomain"
    pDomain.FieldType = esriFieldTypeString
    pDomain.Description = "Sub Region Domain"
    
    'Set the domain's coded values
    Dim pCodedValueDomain As ICodedValueDomain
    Set pCodedValueDomain = pDomain
    pCodedValueDomain.AddCode "E N Cen", "North-East Central Zone"
    pCodedValueDomain.AddCode "E S Cen", "South-East Central Zone"
    pCodedValueDomain.AddCode "Mid Atl", "Mid Atlantic Zone"
    pCodedValueDomain.AddCode "Mtn", "Mountain Zone"
    pCodedValueDomain.AddCode "N Eng", "New England Zone"
    pCodedValueDomain.AddCode "Pacific", "Pacific Zone"
    pCodedValueDomain.AddCode "S Atl", "South Atlantic Zone"
    pCodedValueDomain.AddCode "W N Cen", "North-West Central Zone"
    pCodedValueDomain.AddCode "W S Cen", "South-West Central Zone"
    
    'Add the domain to the workspace
    pWorkspaceDomains2.AddDomain pCodedValueDomain
    
    'Return the domain
    Set GetDomain = pCodedValueDomain
End Function