Geographic Information Systems Stack Exchange is a question and answer site for cartographers, geographers and GIS professionals. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I have the code below, which is supposed to create a new featureclass in memory then add the FC to the map, but I get a Automation error when I use the createfeatureclass method

Set pFC = pFeatWS.CreateFeatureClass("NewBus", pFields, Nothing, Nothing, esriFTSimple, "Shape", " ")

Here' s the full code

Public Sub InMemoryWorkspace()

'Create Workspace factory
Dim pWorkspaceFactory As IWorkspaceFactory2
Set pWorkspaceFactory = New InMemoryWorkspaceFactory

'Create workspace
Dim pWorkspaceName As IWorkspaceName2
Set pWorkspaceName = pWorkspaceFactory.Create("", "MyInMemoryworkspace", Nothing, 0)

'Get a handle on Name
Dim PName As IName
Set PName = pWorkspaceName

'Get handle on Featureworkspace
Dim pFeatWS As IFeatureWorkspace
Set pFeatWS = PName.Open

'Create field object and set number of fields
Dim pFields As IFields
Set pFields = New Fields
Dim pFieldsEdit As IFieldsEdit
Set pFieldsEdit = pFields
pFieldsEdit.FieldCount = 3

'Create objectID field
Dim pField As IField
Set pField = New Field
Dim pFieldEdit As IFieldEdit
Set pFieldEdit = pField
With pFieldEdit
    .Name = "OBJECTID"
    .Type = esriFieldTypeOID
End With
Set pFieldsEdit.Field(0) = pField

'Create postcode field

Set pField = New Field

Set pFieldEdit = pField
With pFieldEdit
    .Name = "POSTCODE"
    .Type = esriFieldTypeString
End With
Set pFieldsEdit.Field(1) = pField

'Create Shape field
Set pField = New Field
Set pFieldEdit = pField
With pFieldEdit
    .Name = "SHAPE"
    .Type = esriFieldTypeGeometry
End With
Set pFieldsEdit.Field(2) = pField

Dim pGeomDef As IGeometryDef
Dim pGeomDefEdit As IGeometryDefEdit
Set pGeomDef = New GeometryDef
Set pGeomDefEdit = pGeomDef
With pGeomDefEdit
  .GeometryType = esriGeometryPolygon
  Set .SpatialReference = New UnknownCoordinateSystem
End With
Set pFieldEdit.GeometryDef = pGeomDef
pFieldsEdit.AddField pField

' Create featureclass
 Dim pFC As IFeatureClass
 Set pFC = pFeatWS.CreateFeatureClass("NewBus", pFields, Nothing, Nothing, esriFTSimple, "Shape", " ")

 Dim pmemLayer As IFeatureLayer
 Set pmemLayer.featureClass = pFC

 Dim pmxdoc As IMxDocument
 Set pmxdoc = ThisDocument
 Dim pmap As IMap
 Set pmap = pmxdoc.FocusMap

 pmap.AddLayer pFC

Can someone explain where I am going wrong.


share|improve this question
I'm wanting to do something similar to this using Python/ArcPy but it looks like my only hope will an enhancement in a future version:… – PolyGeo Mar 22 '12 at 5:13
up vote 2 down vote accepted

Try calling SetDomain on the spatial reference. See code here.

share|improve this answer
Thanks Kirk, that helped. – MapMan Mar 22 '12 at 10:34

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.