Adding Automuber Field with code

R

Roy Goldhammer

Hello there

I have table.

Is there a way to add auto number field to table with vb code?
 
R

Roy Goldhammer

Thankes Jamie

It works.

The reason i need it is for importing data from excel. In the import from
excel wizart there is a way to add key field (an autonumber)

Is there a way to do this with code?
 
T

Terry

Interesting topic. Assuming that the Excel import already has a unique key
what would be the SQL to change that imported column to be the primary key
within the Access table?
Regards

onedaywhen said:
Roy said:
The reason i need it is for importing data from excel. In the import from
excel wizart there is a way to add key field (an autonumber)

Is there a way to do this with code?

With CurrentProject.Connection
.Execute _
"SELECT *" & _
" INTO MyNewTable" & _
" FROM [Excel 8.0;HDR=YES;IMEX=1;Database=C:\db.xls].[Sheet1$];"
.Execute _
"ALTER TABLE MyNewTable ADD MyNewCol" & _
" INTEGER IDENTITY(1,1);"
End With

Jamie.
 
T

Terry

Great stuff, thanks Jamie.
Regards

Jamie Collins said:
Assuming that the Excel import already has a unique key
what would be the SQL to change that imported column to be the primary key
within the Access table?

With CurrentProject.Connection
.Execute _
"SELECT key_col, data_col" & _
" INTO MyNewTable" & _
" FROM [Excel 8.0;HDR=YES;IMEX=1;Database=C:\db.xls].[Sheet1$];"
.Execute _
"ALTER TABLE MyNewTable ADD CONSTRAINT pk__mynewtable" & _
" PRIMARY KEY (key_col);"
End With

Jamie.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top