Create a new feature class inside a geodatabase workspace


This sample will demonstrate how to create a feature class in a geodatabase workspace. It is important to remember to always specify the spatial reference when creating a feature class at the workspace level.

How to use:
  1. Paste the code into your VB or VBA Application.
  2. Call the function from within your application.
'' createWorkspaceFeatureClass: simple helper to create a featureclass in a geodatabase workspace.
'' NOTE: when creating a feature class in a workspace it is important to assign the spatial
''       reference to the geometry field.
Public Function createWorkspaceFeatureClass(featWorkspace As IFeatureWorkspace, _
                                            Name As String, _
                                            featType As esriFeatureType, _
                                            Optional geomType As esriGeometryType = esriGeometryPoint, _
                                            Optional pfields As IFields, _
                                            Optional pCLSID As UID, _
                                            Optional pCLSEXT As UID, _
                                            Optional ConfigWord As String = "" _
                                            ) As IFeatureClass
  On Error GoTo EH
  Set createWorkspaceFeatureClass = Nothing
  If featWorkspace Is Nothing Then Exit Function
  If Name = "" Then Exit Function
  If (pCLSID Is Nothing) Or IsMissing(pCLSID) Then
    Set pCLSID = Nothing
    Set pCLSID = New UID
    '' determine the appropriate geometry type corresponding the the feature type
    Select Case featType
      Case esriFTSimple
        pCLSID.Value = "esricore.Feature"
        If geomType = esriGeometryLine Then geomType = esriGeometryPolyline
      Case esriFTSimpleJunction
        geomType = esriGeometryPoint
        pCLSID.Value = "esricore.SimpleJunctionFeature"
      Case esriFTComplexJunction
        pCLSID.Value = "esricore.ComplexJunctionFeature"
      Case esriFTSimpleEdge
        geomType = esriGeometryPolyline
        pCLSID.Value = "esricore.SimpleEdgeFeature"
      Case esriFTComplexEdge
        geomType = esriGeometryPolyline
        pCLSID.Value = "esricore.ComplexEdgeFeature"
      Case esriFTAnnotation
        Exit Function
    End Select
  End If
  ' establish a fields collection
  If (pfields Is Nothing) Or IsMissing(pfields) Then
    Dim pFieldsEdit As esriCore.IFieldsEdit
    Set pFieldsEdit = New esriCore.Fields
    '' create the geometry field
    Dim pGeomDef As IGeometryDef
    Set pGeomDef = New GeometryDef
    Dim pGeomDefEdit As IGeometryDefEdit
    Set pGeomDefEdit = pGeomDef
    ' assign the spatial reference
    Dim pSR As ISpatialReference
    Set pSR = New esriCore.UnknownCoordinateSystem
    pSR.SetDomain 0, 21474.83645, 0, 21474.83645
    pSR.SetFalseOriginAndUnits 0, 0, 100000
    '' assign the geometry definiton properties.
    With pGeomDefEdit
      .GeometryType = geomType
      .GridCount = 1
      .gridSize(0) = 10
      .AvgNumPoints = 2
      .hasM = False
      .hasZ = False
      Set .SpatialReference = pSR
    End With
    Dim pField As IField
    Dim pFieldEdit As IFieldEdit
    Set pField = New Field
    Set pFieldEdit = pField
    pFieldEdit.Name = "shape"
    pFieldEdit.AliasName = "geometry"
    pFieldEdit.Type = esriFieldTypeGeometry
    Set pFieldEdit.GeometryDef = pGeomDef
    pFieldsEdit.addField pField
    '' create the object id field
    Set pField = New Field
    Set pFieldEdit = pField
    pFieldEdit.Name = "OBJECTID"
    pFieldEdit.AliasName = "object identifier"
    pFieldEdit.Type = esriFieldTypeOID
    pFieldsEdit.addField pField

    Set pfields = pFieldsEdit
  End If
  ' establish the class extension
  If (pCLSEXT Is Nothing) Or IsMissing(pCLSEXT) Then
    Set pCLSEXT = Nothing
  End If
  ' locate the shape field
  Dim strShapeFld As String
  Dim j As Integer
  For j = 0 To pfields.FieldCount - 1
    If pfields.Field(j).Type = esriFieldTypeGeometry Then
      strShapeFld = pfields.Field(j).Name
    End If
  Set createWorkspaceFeatureClass = featWorkspace.CreateFeatureClass(Name, pfields, pCLSID, _
                             pCLSEXT, featType, strShapeFld, ConfigWord)
  Exit Function
    MsgBox Err.Description, vbInformation, "createWorkspaceFeatureClass"
End Function