Assigning attribute domains to an object class' subtypes


Attribute domains are a valid set 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 can take attribute domains that already exist in a workspace and associate them with fields in a feature class or subtype.

How to use:
  1. If necessary, run the 'Creating attribute domains for a workspace' tip.
  2. Paste the code into your VBA Application and run.
Sub Assign_Domains()
  ' open the workspace
  Dim pFact As IWorkspaceFactory
  Set pFact = New AccessWorkspaceFactory
  Dim pWorkspace As IWorkspace
  Set pWorkspace = pFact.OpenFromFile("C:\Arcgis\arcexe83\ArcObjects Developer Kit\Samples\Geodatabase\Database Tools\Creating and Populating a Geodatabase\greeley.mdb", 0)
  Dim pFeatws As IFeatureWorkspace
  Set pFeatws = pWorkspace
  ' QI for the IWorkspaceDomains interface
  Dim pWSDomains As IWorkspaceDomains
  Set pWSDomains = pWorkspace
  ' get the range domain
  Dim pDomain As IDomain
  Set pDomain = pWSDomains.DomainByName("Angles")
  ' Get the poles feature class
  Dim pFeatds As IFeatureDataset
  Set pFeatds = pFeatws.OpenFeatureDataset("SNUtilities")
  Dim pFeatclscont As IFeatureClassContainer
  Set pFeatclscont = pFeatds
  Dim pFeatcls As IFeatureClass
  Set pFeatcls = pFeatclscont.ClassByName("SNPoles")
  ' Assign the range value domain to the angle field for poles
  Dim pClassEdit As IClassSchemaEdit
  Set pClassEdit = pFeatcls
  pClassEdit.AlterDomain "angle", pDomain
  ' Get the buildings feature class
  Set pFeatds = pFeatws.OpenFeatureDataset("SNLandbase")
  Set pFeatclscont = pFeatds
  Set pFeatcls = pFeatclscont.ClassByName("SNBuildings")
  ' QI for the ISubtypes interface
  Dim pSubType As ISubtypes
  Dim lSubT As Long
  Set pSubType = pFeatcls

  ' Assign the coded value domain to the TYPE_ field for
  ' subtypes 0, 1 and 2 in the building feature class.
  Dim pDomainCV As IDomain
  Set pDomainCV = pWSDomains.DomainByName("Building Types")
  Set pSubType.Domain(0, "TYPE_") = pDomainCV
  Set pSubType.Domain(1, "TYPE_") = pDomainCV
  Set pSubType.Domain(2, "TYPE_") = pDomainCV
  MsgBox "Done"
End Sub