Creating table inside .mdb database using arcpy.CreateTable_management. I have an access mdb database and I want to use arcpy.CreateTable_management to create a table inside the .mdb. How?

7520
24
06-03-2016 08:56 AM
PaulHacker2
New Contributor II

I have an access mdb database and I want to use arcpy.CreateTable_management to create a table inside the .mdb.

This works to make the DB using Python:

config_keyword = ""

mdb = ".mdb"

out_folder_path = "C:/output"

out_name = "CountyCode_MapGrid_"

name = "newDB"

arcpy.CreatePersonalGDB_management(out_folder_path, out_name + name + mdb)

And it works fine but to get a table created INSIDE the .mdb using:

   arcpy.CreateTable_management(out_folder_path, "Block_77", config_keyword)

    arcpy.AddField_management(out_name, "OBJECTID", "AutoNumber")

    arcpy.AddField_management(out_name, "SHAPE", "OLE Object")

    arcpy.AddField_management(out_name, "id_block", "Number")

    arcpy.AddField_management(out_name, "id_mapgrid", "Number")

    arcpy.AddField_management(out_name, "map_number", "Short Text")

    arcpy.AddField_management(out_name, "Block", "Short Text")

    arcpy.AddField_management(out_name, "SHAPE_Length", "Number")

    arcpy.AddField_management(out_name, "SHAPE_Area", "Number")

    arcpy.AddField_management(out_name, "created_user", "Short Text")

    arcpy.AddField_management(out_name, "created_date", "Date/Time")

    arcpy.AddField_management(out_name, "last_edited_user", "Short Text")

    arcpy.AddField_management(out_name, "last_edited_date", "Date/Time")

Does not work. Any suggestions?

Thanks!

0 Kudos
24 Replies
JoshuaBixby
MVP Esteemed Contributor

If you already have a feature class or shapefile and want to copy it into a different folder or geodatabase, check out the Feature Class to Feature Class tool.

PaulHacker2
New Contributor II

Found using ArcMap I can create a  field name "SHAPE"  with data type "Geometry".

How can I do this with Python? THIS is what I need exactly to get the 'OLE Object' to work.

I used arcpy.AddField_management but no go with:

arcpy.AddField_management(tablename, "SHAPE", "Geometry")

Still gives error ERROR 000800: The value is not a member of TEXT | FLOAT | DOUBLE | SHORT | LONG | DATE | BLOB | RASTER | GUID.

Is there another arcpy function for this?

Thanks.

0 Kudos
JoshuaBixby
MVP Esteemed Contributor

How exactly are you adding this "SHAPE" field in ArcMap?  I suspect my reply will still be Create Feature Class tool.

DanPatterson_Retired
MVP Emeritus

Is there another arcpy function for this?     No which is why you use copyfeatures or featureclasstofeatureclass or strip the attributes out of a copy of an existing file.  This is to slow down people would want to produce geometry outside of what esri intends, much like you can do now with arcpy.da 's numpyarraytofeatureclass.  There you can create your own geometry objects but you would have to be working with arrays to begin with.

PaulHacker2
New Contributor II

Ok, the users use ArcMap to create a mdb with a table and inside that table they create field name SHAPE with Geometry for the datatype. They already have this one database and table. They want ME to (for many many new databases each with several tables) take the SHAPE field name (with Geometry for datatype) and copy it's attributes to the other databases and tables so they all have a SHAPE with Geometry.

I have a python script that will take the list of names of the databases they want, it's array, and go through each one creating databases with the same names (using arcpy.CreatePersonalGDB_management), then creating tables with the names they specified to be inside each database (arcpy.CreateTable_management), and inside each of the tables use arcpy.AddField_management to make each field... with the exception of the SHAPE field I need to be as Geometry.

And that is where I'm at. I can get each of the fields created, tables created, and PersonalGDB to execute, but adding that one field, for a Geometry datatype, is the problem.

Thanks, Paul

0 Kudos
PaulHacker2
New Contributor II

New information. The users use ArcGis 10.1 and they create their mdb using the toolboxs. They create a personal Geodatabase. It seems it somehow creates a SHAPE field with Geography for the field type.

Looks like this:

  Field Name          DataType                              Description(optional)

-----------------------------------------------------------------------------------------------------------

OBJECTID           AutoNumber

SHAPE                 OLE Object  <-- the geography

id_block                 Number

id_mapgrid             Number

etc..

Now such as id_block they input but the SHAPE and OBJECTID is automatically created.

How was this created? What Python tool was used?

I already use arrcpy.CreatePersonalGDB_management   - to make the new mdb

arcpy.CreateTable_management                                        - to create the table

and

arcpy.AddField_management                                             - to create the fields

But nowhere does the SHAPE field show up. Now I see a CreateFeatureclass_management but how does that make a field in just one table of the mdb?

Thanks,

Paul

0 Kudos
JoshuaBixby
MVP Esteemed Contributor

I don't know how many more ways people can say it, you don't create a spatially-enabled table in either a personal (*.MDB) or file (*.GDB) geodatabase by using Create Tool and Add Field.  As you have already found, the proper method to create a spatially-enabled table (one with a SHAPE field) is to use the Create Feature Class tool.  The tool does multiple things for you, one it creates a table, and two it adds the spatial field.  The result is a new table with a SHAPE field already defined for you.

In terms of how, specifically, Create Feature Class makes the SHAPE field, the answer is buried in the code of the tool.  I think for your purposes, all you need to know is that the tool works and you should be working with it.

If your goal is to create a SHAPE field in a Access table not using Esri tools, the short answer is don't.

RebeccaStrauch__GISP
MVP Emeritus

Can you take a step back and explain what your actually goal is?  I think you are getting confused about just a table vs a featureclass.  A table can be just that, a list of fields with data in them.  Having a field called shape does not make it into a featureclass in and of itself.  Read thru the links that Joshua and Dan sent.  If you create a featureclass, ArcGIS (whether thru a toolbox or arcpy) will add the Shape field and will contain the geometry.  What geometry is included in the Shape is determined by you input.

if you have a table that contains XY coordinates, you can generate a point featureclass, but you can also import from many other formats (Shape files, coverages, autocad, raster formats, etc) to create a featureclass.

if you have a featureclass with geometry and have another table with attributes that you want tohave attached to the features with a common field, use JoinTable_managerment  (sorry can't add the direct link...will come back and add).

in anycase, unless there is a business requirement for you having to stay in and mdb personal database...I highly recomend you change to using the file geodatabase instead. Much more reliable and made for ArcGIS instead of relying on Access and it's limits (and version changes).

So what is you ultimate goal and what are your resources?  are they just tables with columns and data or is there some sort of coordinate information or geometry in the tables already.  Again, you don't want to add a Shape field and try to create a featureclass that way.  what format was it in originally that has the geometry in an "OLE" field?  maybe I'm missing something?

edit link to join table management for 10.1  ArcGIS Help 10.1

PaulHacker2
New Contributor II

Sorry gang. Here is the story.

The users use ArcGis to make this to begin with. It ends up with a .mdb database. It already has in the mdb the tables GDB_ColumnInfo, GDB_GeomColumns, GDB_ItemRelationships, GDB_ItemRelationshipTypes,  GDB_Items,  GDB_Items_Shape_Index, GDB_ItemTypes, GDB_ReplicaLog, GDB_SpatialRefs, as well as the tables they created using Arcgis.

In their tables, many of these tables, each with a name as well as a number, and that number corresponds to land tracts, have inside them field names such as OBJECTID (AutoNumber), and SHAPE (OLE Object.. and I know it is supposed to be a polygon), and other fields they have defined with Data Types Number, Short Text, Date/Time..

Now while they have done ONE such MDB, they want me to duplicate this maybe 500 times for many other tracts.

Yes many databases.

I am using Python and CreateFeatureclass_management

OR

CreatePersonalGDB_management/CreateTable_management/AddField_management (which works for everything but the SHAPE field... can't get a POLYGON in it using AddField.)

Each of the databases will have unique names, and most of the tables will have unique names, and most of the fields will have unique names depending on the tract number!

I have a txt file with all the tract numbers and I read it in via Python open and 'for line in f.readlines' and an array to hold them.

So I can do all the renaming easily, it's the SHAPE field that drives me nuts!

Oh, and I am a MVS/CICS/COBOL Mainframer turned Visual Studio/C#/SQL programmer & ArcGIS/Python programmer. And this is my first Python program! Everything works except that SHAPE field...

Now if I can just find some way to use the same CreatePersonalGDB_management/CreateTable_management/AddField_management  to make the database and then in the tables slip in a SHAPE that is a polygon, that would do what I need to be done.

Thanks!

0 Kudos
WesMiller
Regular Contributor III