> No, a TableDef is the object for an existing table. If you
> delete it, the reference to the TableDef object will fail
[quoted text clipped - 49 lines]
> >> I would have expected the Set tdfEmployees line to fail
> >> because you just deleted the table.
>I add the following statment after the CreateTabledef1 label and received
>the same error: Set tdfEmployees = MyDatabase.CreateTabledef("Employees").
[quoted text clipped - 8 lines]
>If I have a lot of fields instead of creating them manually, I would rather
>do something like this if possible.
Since you deleted the TableDef, then yes,when you
(re)create the TableDef, you have to r(e)create the fields,
properties, indexes, etc.
If this is a "one" time operation, how about creating the
table manually using the table design window? When you want
to test you can Copy/Paste the "template" table (in the DB
window) to make a fresh version of the working table.

Signature
Marsh
MVP [MS Access]
jbruen - 24 Feb 2005 15:33 GMT
Marshall
The reason I'm trying to do this is I want to create a table in code instead
of from the design view with the correct properties.
If i use the CreateTable first and then call the code below, I receive:
"There are several tables with that name. Please specify owner in the format
'owner.table'."Set NewTable = MyDatabase.TableDefs("TabNewWiring2")"
statement.
If I take an existing table and remove all the fields except for 1, run the
following it works. If I create a table from scratch in the design view with
1 field and run the same thing, I recieve "Item not found in this collection"
on the "Set NewTable = MyDatabase.TableDefs("TabNewWiring2") statement. What
is the reason for this?
Function CreateOthers()
Dim MyDatabase As Database, NewTable As TableDef
Dim fldTemp As Field
On Error GoTo CreateOtherdef
Set MyDatabase = DBEngine.Workspaces(0).Databases(0)
Set NewTable = MyDatabase.TableDefs("TabNewWiring2")
' Create a new Field object and append it to the Fields
' collection of the Employees table.
Set fldTemp = NewTable.CreateField("Field2", dbText, 10)
fldTemp.AllowZeroLength = True
NewTable.Fields.Append fldTemp
DoCmd.SetWarnings True
Exit Function
CreateOtherdef:
Debug.Print Error$
Stop
End Function
> >I add the following statment after the CreateTabledef1 label and received
> >the same error: Set tdfEmployees = MyDatabase.CreateTabledef("Employees").
[quoted text clipped - 17 lines]
> to test you can Copy/Paste the "template" table (in the DB
> window) to make a fresh version of the working table.
jbruen - 24 Feb 2005 16:01 GMT
Marshal
Once I changed the Set MyDB from DBEngine.Workspaces(0).Databases(0) to
OpenDatabase("h:\access\hsbb\HSBB.mdb"), everything worked fine. I was able
to use the following:
Function CreateTable()
Dim MyDatabase As Database, NewTable As TableDef
Set MyDatabase = OpenDatabase("h:\access\hsbb\HSBB.mdb")
DoCmd.SetWarnings False
On Error GoTo CreateTabledef
DoCmd.DeleteObject acTable, "TabNewWiring"
CreateTabledef:
On Error GoTo CreateTableErr
Set NewTable = MyDatabase.CreateTabledef("TabNewWiring")
With NewTable
.Fields.Append .CreateField("RefNO", dbLong)
End With
MyDatabase.TableDefs.Append NewTable
Call CreateOthers
DoCmd.SetWarnings True
Exit Function
CreateTableErr:
Stop
End Function
Function CreateOthers()
Dim MyDatabase As Database, NewTable As TableDef
Dim fldTemp As Field
On Error GoTo CreateOtherdef
Set MyDatabase = OpenDatabase("h:\access\hsbb\HSBB.mdb")
Set NewTable = MyDatabase.TableDefs("TabNewWiring")
Set fldTemp = NewTable.CreateField("NewDConnections", dbBoolean)
NewTable.Fields.Append fldTemp
DoCmd.SetWarnings True
Exit Function
CreateOtherdef:
DoCmd.SetWarnings True
Debug.Print Error$
Stop
End Function
John
> >I add the following statment after the CreateTabledef1 label and received
> >the same error: Set tdfEmployees = MyDatabase.CreateTabledef("Employees").
[quoted text clipped - 17 lines]
> to test you can Copy/Paste the "template" table (in the DB
> window) to make a fresh version of the working table.
Marshall Barton - 25 Feb 2005 05:20 GMT
It sounds like you got it to work.
You've kind of lost me with all the different things you've
tried. Just remember that you can only use CreateTableDef
if it doesn't already exist. And you can only reference an
already existing TableDef.
What I was suggesting before by precreating a table in
design view is to then use it as a "template". Then when
you wanted a fresh copy of the empty table with all its
fields and their properties, all you would have to do is
delete any existing copy and make a new copy from the
"template" using:
DoCmd.CopyObject , TabNewWiring, acTable, TemplateTable

Signature
Marsh
MVP [MS Access]
>Once I changed the Set MyDB from DBEngine.Workspaces(0).Databases(0) to
>OpenDatabase("h:\access\hsbb\HSBB.mdb"), everything worked fine. I was able
[quoted text clipped - 60 lines]
>> to test you can Copy/Paste the "template" table (in the DB
>> window) to make a fresh version of the working table.