Error from AccessWorkspaceFactory.Create

948
8
05-30-2012 06:26 AM
JohnFritzen
New Contributor
Hello,

For the following code....
 Dim pWorkspaceName As ESRI.ArcGIS.Geodatabase.IWorkspaceName
 Dim pWorkspaceFactory As ESRI.ArcGIS.Geodatabase.IWorkspaceFactory
 'Set up new database
 pWorkspaceFactory = New ESRI.ArcGIS.DataSourcesGDB.AccessWorkspaceFactory
 pWorkspaceName = pWorkspaceFactory.Create("C:\Temp", "foo.mdb", Nothing, 0)


I get the error...
System.Runtime.InteropServices.COMException was caught
  ErrorCode=-2147220973
  Message=Exception from HRESULT: 0x80040213
  Source=ESRI.ArcGIS.DataSourcesGDB
  StackTrace:
       at ESRI.ArcGIS.DataSourcesGDB.AccessWorkspaceFactoryClass.IWorkspaceFactory2_Create(String parentDirectory, String Name, IPropertySet ConnectionProperties, Int32 hWnd)


I am using vs2010 and ArcMap 10. 
The code is in a windows form.  The form is called from a ICommand class.  My code was converted from VB6 and ESRI 9.x.

When I am debugging, this line and step into it

pWorkspaceFactory = New ESRI.ArcGIS.DataSourcesGDB.AccessWorkspaceFactory

It goes to the Deactivate subroutine of the Form code.

Thanks for your help!
0 Kudos
8 Replies
RichWawrzonek
Occasional Contributor
This problem comes up frequently in this forum.

Workspaces are singleton objects and you should use the activator class to instantiate them. See documentation:
http://help.arcgis.com/en/sdk/10.0/arcobjects_net/conceptualhelp/index.html#//00010000043p000000

In your case the line:
pWorkspaceFactory = New ESRI.ArcGIS.DataSourcesGDB.AccessWorkspaceFactory


should be replaced with:

pWorkspaceFactory = CType(Activator.CreateInstance(Type.GetTypeFromProgID("esriDataSourcesGDB.AccessWorkspaceFactory")), IWorkspaceFactory)
0 Kudos
JohnFritzen
New Contributor
Hi Rich,

Thanks for your help.  The documentation on singleton objects was also very helpful.

Unfortunately the problem persists.
0 Kudos
RichWawrzonek
Occasional Contributor
You need to make sure the specified directory (C:\Temp in your case) already exists. The IWorkspaceFactory.Create() method will not create it for you.
0 Kudos
JohnFritzen
New Contributor
Yes, that dir exists.
0 Kudos
RichWawrzonek
Occasional Contributor
Is this an ArcGIS COM extension or a standalone app?
0 Kudos
JohnFritzen
New Contributor
It is a COM extension.  Runs in a windows form launched from an icommand class.
0 Kudos
NeilClemmons
Regular Contributor III
There are several things that could cause an issue here.

 Dim pWorkspaceName As ESRI.ArcGIS.Geodatabase.IWorkspaceName
 Dim pWorkspaceFactory As ESRI.ArcGIS.Geodatabase.IWorkspaceFactory
 'Set up new database
 pWorkspaceFactory = New ESRI.ArcGIS.DataSourcesGDB.AccessWorkspaceFactory
 pWorkspaceName = pWorkspaceFactory.Create("C:\Temp", "foo.mdb", Nothing, 0)


As already mentioned, use the Activator class to create an instance of the workspace factory.  Also mentioned, the directory C:\Temp must already exist.  Additionally, the database foo.mdb should not already exist.  I don't think the Create method will overwrite an existing database.  The final parameter to Create is a window handle.  Since you're calling this method from a Windows Form, you should be passing in the handle of that form (Form.Handle.ToInt32).  In cases where the code is not called from a form, use the application's handle (IApplication.hWnd).  You also need to have write permissions on the Temp directory.  I can't imagine why you wouldn't but if the code is still failing you might want to make sure.
0 Kudos
JohnFritzen
New Contributor
Thanks for your help.  I did try a version of the code passing a window handle.  I also tried passing and empty property object instead of "Nothing".
0 Kudos