Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Copy structure of table to create empty table 3

Status
Not open for further replies.

Dan01

Programmer
Jun 14, 2001
439
0
0
US
Hi, is there a way to copy a table's structure to a new table without bringing the data along? Thanks, Dan.
 
Copy a table's structure using a make-table query

SELECT Table1.* INTO table2
FROM Table1
WHERE (((False)<>False));
 
Saanich, that solution is pretty awesome.

Would you also know how to add an autonumber field to the empty table? Thanks, Dan.
 
You can also just copy the table (click on it and Ctrl+C). Then past the table (Ctrl+V) and it will ask if you want structure or structure and data. You can just insert the autonumber field from the table design screen.
HTH
Kimberly
 
Thanks Kimberly. Trying to automate this, however. Dan.
 
Use the data definition sql to add fields to a table. You can find examples of it in help.
 
JonFer, I believe there is an ALTER TABLE sql and a DAO Create Fields process, but in both cases I don't know how to add autonumber fields. Thanks, Dan.
 
Here are the two ways.

1. Data def query using field type of &quot;AutoIncrement&quot; (not documented well - found by experimentation)

ALTER TABLE Wells ADD AutoNum AutoIncrement NOT NULL ;

2. VB

Sub AlterTable()

Dim db1 As Database
Dim td1 As TableDef
Dim fd1 As Field

Set db1 = CurrentDb
Set td1 = db1.TableDefs(&quot;TableName&quot;)

' create a new field of type Long and set
' the autonumber attribute. The default is
' to increment the value

Set fd1 = td1.CreateField(&quot;AutoNum&quot;, 4)
fd1.Attributes = dbAutoIncrField
td1.Fields.Append fd1

End Sub
 
Thanks Jonfer. Great findings! Dan.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top