Full Version: Autonumber field using SQL
UtterAccess Discussion Forums > Microsoft® Access > Access Tables + Relationships
cottonl
Is there any way to create an auto number field using SQL?

What I'm doing:

I've created a query which creates a table I want the table to have a field that has a unique ID (auto number field) that every time the query gets preformed gets refreshed hence creating an auto number field in SQL in the original query...

Hope this makes since and hope someone can help...

Thanks!

Lydia
Larry Larsen
Hi
Here is some SQL that creates a table with an autonumber and a couple of fields, you could run this first then append your records after..
CODE
Sub sCreate1()
    Dim db As Database
    Dim strSQL As String
    Set db = DBEngine(0)(0)
    strSQL = "CREATE TABLE [Table1New] (FieldPK COUNTER CONSTRAINT PrimaryKey PRIMARY KEY, FieldOne TEXT, FieldTwo TEXT );"
    db.Execute strSQL
    Set db = Nothing
End Sub

HTH's
thumbup.gif
ChrisP
Or you could do it the other way around.

this sql code should add an autonumber field that is the primary key to an exisiting table:

ALTER TABLE temp ADD COLUMN id COUNTER CONSTRAINT pkey PRIMARY KEY
cottonl
Great thanks!

It works Great!

Lydia
This is a "lo-fi" version of our main content. To view the full version with more information, formatting and images, please click here.