Adding Automuber Field with code

  • Thread starter Thread starter Roy Goldhammer
  • Start date Start date
R

Roy Goldhammer

Hello there

I have table.

Is there a way to add auto number field to table with vb code?
 
Roy said:
Is there a way to add auto number field to table with vb code?

CurrentProject.Connection.Execute _
"ALTER TABLE MyTable ADD MyNewCol INTEGER IDENTITY(1,1);"

Jamie.

--
 
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?
 
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.

--
 
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.
 
Terry 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.

--
 
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.
 
Back
Top