6

I'm trying the following:

CREATE TABLE Table1
(
    RecordNo autonumber, --error here!
    PersonId varchar(50),
    ...
)

But, there is an error.
How can I build the correct query in Access?

Shin
  • 664
  • 2
  • 13
  • 30
Gopal
  • 11,712
  • 52
  • 154
  • 229

4 Answers4

5

According to SQL Auto Increment a Field:

CREATE TABLE Persons
(
P_Id PRIMARY KEY AUTOINCREMENT,
LastName varchar(255) NOT NULL,
FirstName varchar(255),
Address varchar(255),
City varchar(255)
)

The MS Access uses the AUTOINCREMENT keyword to perform an auto-increment feature.

By default, the starting value for AUTOINCREMENT is 1, and it will increment by 1 for each new record.

To specify that the "P_Id" column should start at value 10 and increment by 5, change the autoincrement to AUTOINCREMENT(10,5).

Synonyms for AUTOINCREMENT include COUNTER and IDENTITY. Using IDENTITY the makes a lot of sense because it matched the @IDENTITY variable which returns the last used autonumber value.

onedaywhen
  • 55,269
  • 12
  • 100
  • 138
Eugene Yokota
  • 94,654
  • 45
  • 215
  • 319
3

The order might be important

CREATE TABLE Persons
( pkObject AUTOINCREMENT PRIMARY KEY)

If I try PRIMARY KEY AUTOINCREMENT as suggested, it gets upset (MSAccess 2010).

Pang
  • 9,564
  • 146
  • 81
  • 122
Robbie
  • 61
  • 6
1

Method 1:

  1. Open table in design view
  2. Make a field named "ID" or whatever the field will be that will have the Auto Increment
  3. Put "AutoNumber" under DataType

Method 2:

  1. Make a new table
  2. Close the table and save it
  3. When it asks if you want a primary key click ok
  4. Open the table in Design View
  5. Edit the new field to whatever name you like
1

When using ancient DAO 3.60 and Jet 4.0 with Access 2003 files, Eugene Yokota'a syntax did not work. I found that COUNTER keyword will do the trick:

CREATE TABLE tablename(id COUNTER, Name Text (30))

Thanks to this post: http://www.vbforums.com/showthread.php?234335

Rumi
  • 1,290
  • 1
  • 11
  • 17