View Full Version : DELETE ID from many tables


editolis
03-12-2009, 05:40 AM
Hi all,
I am importing data from XL file and then I create 50 new tables.
Each table automatically creates ID.
I am using ACCESS 2007 and EXCEL 2007.
For some reason I do not want the ID.
My question is this:
How can I AUTO - delete the ID from each table.
I do not want to do it “by hand” in each one table.
Can you suggest me any simple solution? Maybe qry or VBA.
Thank you in advance.

Dennisk
03-12-2009, 05:48 AM
if you are referring to an auto id, either ignore it, or go into table design and delete it.

In my opinion all M$ software does too much second guessing especially when it comes to indexing and auto numbers. When you created the table it will have prompted you to have M$ Access automaticcally create a Primary Key for you, if you click yes it creates an auto number.

editolis
03-12-2009, 12:55 PM
I Try this but does not working:

Dim db As Database

db.TableDefs![tblName].Fields.Delete "ID"
db.Close
Set db = Nothing

Set db = CurrentDb()
db.TableDefs![tblName].Refresh
Set db = Nothing

Any other suggestions?

boblarson
03-12-2009, 01:18 PM
You need to use the ALTER TABLE SQL to remove fields.

Dim strSQL As String

strSQL = "ALTER TABLE YourTableNameHere DROP COLUMN ID"

CurrentDb.Execute strSQL, dbFailOnError

editolis
03-12-2009, 01:50 PM
You need to use the ALTER TABLE SQL to remove fields.

Dim strSQL As String

strSQL = "ALTER TABLE YourTableNameHere DROP COLUMN ID"

CurrentDb.Execute strSQL, dbFailOnError


Dear BOB,

Thank you for your reply.

That give me the error Run-Time error '3280'

Is working perfect for other COLUMNS. Not for the ID.

boblarson
03-12-2009, 01:53 PM
Hmm - you might need to drop any indexes first.

editolis
03-12-2009, 02:23 PM
Hmm - you might need to drop any indexes first.

Dear BOB,

I Try this but i have error:

drop index TableName.ID

What i am doing wrong?

boblarson
03-12-2009, 02:26 PM
For Access it would be:

DROP INDEX PrimaryKey ON YourTableNameHere

gemma-the-husky
03-12-2009, 02:27 PM
why on earth are you creating 50 (exactly 50?) new tables

that just cant be right.

what info is on your import file?

editolis
03-12-2009, 02:32 PM
why on earth are you creating 50 (exactly 50?) new tables

that just cant be right.

what info is on your import file?

I Am importing RANKINK tables from football teams
all over the world.

boblarson
03-12-2009, 02:33 PM
Oh, and a good reference for you is:
http://www.w3schools.com/SQL/sql_drop.asp

editolis
03-12-2009, 02:38 PM
Dear Bob,

Here is the code but i get error:

Dim strSQL As String

DROP INDEX ID ON tblBrazil

strSQL = "ALTER TABLE tblBrazil DROP COLUMN ID"

CurrentDb.Execute strSQL, dbFailOnError

I Think you are close to the solution.

boblarson
03-12-2009, 02:43 PM
Dear Bob,

Here is the code but i get error:

Dim strSQL As String

DROP INDEX ID ON tblBrazil

strSQL = "ALTER TABLE tblBrazil DROP COLUMN ID"

CurrentDb.Execute strSQL, dbFailOnError

I Think you are close to the solution.

1. there would be no spaces in the index name.

2. If you didn't add the index yourself and let Access add the "primary key" then use my code EXACTLY as shown as it adds the index with the name PrimaryKey:


Dim strSQL As String

DROP PrimaryKey ON tblBrazil

strSQL = "ALTER TABLE tblBrazil DROP COLUMN ID"

CurrentDb.Execute strSQL, dbFailOnError

editolis
03-12-2009, 02:58 PM
The code give me this error:

DROP ID ON tblBrazil

Compile error:

expected: end of statement

=============================

Dear Bob thank you for your help.

We can try again tomorrow.

boblarson
03-12-2009, 03:00 PM
The code give me this error:

DROP ID ON tblBrazil

Compile error:

expected: end of statement

=============================

Dear Bob thank you for your help.

We can try again tomorrow.

Why did you use this code:
DROP ID ON tblBrazil

I said use what I gave you EXACTLY AS WRITTEN

DROP PrimaryKey On tblBrazil

Do not change a word of it, leave the words PrimaryKey exactly as written.

editolis
03-12-2009, 03:05 PM
The code give me again this error:

DROP PrimaryKey ON tblBrazil

Compile error:

expected: end of statement.

boblarson
03-12-2009, 03:12 PM
oops I think we needed:


Dim strSQL As String

strSQL = "DROP PrimaryKey ON tblBrazil"

CurrentDb.Execute strSQL, dbFailOnError

strSQL = "ALTER TABLE tblBrazil DROP COLUMN ID"

CurrentDb.Execute strSQL, dbFailOnError

editolis
03-12-2009, 03:16 PM
Syntax error in DROP TABLE or DROP INDEX. (Error 3295)
You entered an SQL statement that has an invalid DROP statement.
Possible causes:



A reserved word or argument name is misspelled or missing.
Punctuation is incorrect.

boblarson
03-12-2009, 03:17 PM
Well, I guess tomorrow it is... :D

editolis
03-12-2009, 03:21 PM
Here in Athens the time now is 1.20 midnight.

Goodnight.

Thank you

boblarson
03-12-2009, 03:27 PM
Get a good night's sleep. Of course, I had to get it right after that :D

Dim strSQL As String

strSQL = "DROP INDEX PrimaryKey ON tblBrazil"

CurrentDb.Execute strSQL, dbFailOnError

strSQL = "ALTER TABLE tblBrazil DROP COLUMN ID"

CurrentDb.Execute strSQL, dbFailOnError

editolis
03-13-2009, 01:57 AM
Dear Bob,

Finally this is the solution:

Dim strSql

strSql = "DROP INDEX PrimaryKey ON tblBrazil"

CurrentDb.Execute strSql, dbFailOnError

strSql = "ALTER TABLE tblBrazil DROP COLUMN ID"

CurrentDb.Execute strSql, dbFailOnError

Thank you for your time and your help.