Creating attribute domains for a workspace

Created:1/20/1999
Description:

Attribute domains are a valid set or range of values that can be stored in a field. Domains allow you to constrain the values for an attribute, providing more control than that given by the datatype of the field. Domains are created and stored in the workspace, but used with a table, feature class or subtype. To use these attribute domains as attribute rules, you must associate them with a field from an object class or subtype.



The following example creates 2 different attribute domains, a range domain and a coded value domain:

The range domain 'Angles' allows values from 0 to 360. The coded value domain 'Building Types' allows the following and only the following values:



BLD - Unclassified Building

COM - Commercial Building

RES - Residental Building



How to use:
  1. If necessary, Run the 'Creating and Populating a Geodatabase' sample from the Database Tools section. This will build the Greeley geodatabase.
  2. Paste the code into your VBA Application and Run
Sub Create_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

  ' create the range domain
  Dim pRangeDomain As IRangeDomain
  Set pRangeDomain = New RangeDomain
  pRangeDomain.MinValue = 0
  pRangeDomain.MaxValue = 360

  ' QI for IDomain to set its name, field type, split/merge policies
  Dim pDomain As IDomain
  Set pDomain = pRangeDomain
   With pDomain
    .Name = "Angles"
    .Description = "Valid angle range"
    .FieldType = esriFieldTypeDouble
    .SplitPolicy = esriSPTGeometryRatio
    .MergePolicy = esriMPTSumValues
  End With

  ' QI the workspace object for its IWorkspaceDomains interface
  ' and add the domain to the workspace if it doesn't exist already
  Dim pWSDomains As IWorkspaceDomains
  Set pWSDomains = pWorkspace
  ' check if domain exists
  Dim pExistingDomain As IDomain
  Set pExistingDomain = pWSDomains.DomainByName("Angles")
  If pExistingDomain Is Nothing Then
    pWSDomains.AddDomain pDomain
  End If
  
  ' create the coded value domain
  Dim pCVDomain As ICodedValueDomain
  Set pCVDomain = New CodedValueDomain
  With pCVDomain
    .AddCode "BLD", "Unclassified Building"
    .AddCode "COM", "Commercial Building"
    .AddCode "RES", "Residental Building"
  End With

  ' QI for IDomain to set its name, field type, split/merge policies
  Set pDomain = pCVDomain
  With pDomain
    .FieldType = esriFieldTypeString
    .Name = "Building Types"
    .Description = "Types of Buildings"
    .SplitPolicy = esriSPTDuplicate
    .MergePolicy = esriMPTDefaultValue
  End With
  
  ' Add the domain to the workspace, if domain doesn't already exist
  Set pExistingDomain = pWSDomains.DomainByName("Building Types")
  If pExistingDomain Is Nothing Then
    pWSDomains.AddDomain pDomain
  End If
  
  MsgBox "Done"

End Sub