473,503 Members | 10,245 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

How to create an AutoNumber field with a SQL statement?

I need to upgrade a MS Access database (Version 2002) with a script
only, i.e., via SQL statement, not via Access GUI, and I'm having
trouble defining an AutoNumber field. I got an exception error when
running the follwoing (in a C++ program using ADO):

......
cmdStr = "CREATE TABLE mytab "
"([Id] AutoNumber, "
"[Desc] TEXT(50), "
"CONSTRAINT [PK_MYTAB] PRIMARY KEY ([Id])); ";
pConn->Execute(cmdStr, NULL, adExecuteNoRecords);

When I changed the "Id" column data type to "integer", the program
updated the Access database successfully. However, the "id" field now
becomes a regular integer field. How do I make it an AutoNumber field
with a SQL statement?

Thanks.

Mar 13 '06 #1
9 60058
Bri


mi****@excite.com wrote:
I need to upgrade a MS Access database (Version 2002) with a script
only, i.e., via SQL statement, not via Access GUI, and I'm having
trouble defining an AutoNumber field. I got an exception error when
running the follwoing (in a C++ program using ADO):

......
cmdStr = "CREATE TABLE mytab "
"([Id] AutoNumber, "
"[Desc] TEXT(50), "
"CONSTRAINT [PK_MYTAB] PRIMARY KEY ([Id])); ";
pConn->Execute(cmdStr, NULL, adExecuteNoRecords);

When I changed the "Id" column data type to "integer", the program
updated the Access database successfully. However, the "id" field now
becomes a regular integer field. How do I make it an AutoNumber field
with a SQL statement?

Thanks.


I thought this was going to be an easy one. SQL Server and MySQL both
have a syntax for this but Access doesn't seem to. At least I can't find
it either. In these you define the Type as Long and then an extra
parameter makes it an Identity. The Access Help doesn't mention this
parameter, so it may not exist.

I did find that you can do it with the CreateTable method of the
DAO.Database object with an Attribute parameter. I don't know if that
helps or not. There appears to be a lot more options to CreateTable than
the SQL Create Table.

--
Bri

Mar 13 '06 #2
rkc

AUTOINCREMENT
Mar 13 '06 #3
rkc wrote:
AUTOINCREMENT


Thank you guys very much! AUTOINCREMENT worked!

Mar 14 '06 #4
Bri

rkc wrote:

AUTOINCREMENT


Excellent! I searched through the Create Table help and it didn't have
specific info on Data Type. The Data Type help calles it AutoNumber not
AutoIncrement. Where did you find that AutoIncrement was the correct
Type? Sometimes help needs help.

--
Bri

Mar 14 '06 #5
rkc
Bri wrote:

rkc wrote:

AUTOINCREMENT

Excellent! I searched through the Create Table help and it didn't have
specific info on Data Type. The Data Type help calles it AutoNumber not
AutoIncrement. Where did you find that AutoIncrement was the correct
Type? Sometimes help needs help.


I have no idea where I learned half of what I know. I dug that bit
of info out of a class that writes create table ddl to an html file.

A Google search now that you know should reveal several resources.

I just did a search and discovered that COUNTER also works.
Mar 15 '06 #6
TTBOMR, when MS-Access went from JET 3.5 to JET 4.0, it was also the
time when ADPs and MSDE were at the fore-front of the glitz message
from MS. Either they forgot to tell us about JET 4.0, or many of us,
including me, forgot to look.
The !!!!!new!!!!! (6 years old?) thingmes in JET 4.0 are discussed at
http://support.microsoft.com/kb/q275561/
but not as extensively as one might wish.
JET 4.0 has some great !!!!!new!!!!! capabilities, but as I see it,
they are 99.44% ignored in CDMA and elsewhere.

Mar 15 '06 #7
Bri
Lyle Fairfield wrote:
TTBOMR, when MS-Access went from JET 3.5 to JET 4.0, it was also the
time when ADPs and MSDE were at the fore-front of the glitz message
from MS. Either they forgot to tell us about JET 4.0, or many of us,
including me, forgot to look.
The !!!!!new!!!!! (6 years old?) thingmes in JET 4.0 are discussed at
http://support.microsoft.com/kb/q275561/
but not as extensively as one might wish.
JET 4.0 has some great !!!!!new!!!!! capabilities, but as I see it,
they are 99.44% ignored in CDMA and elsewhere.


Since I still do most of my development in AC97, I haven't really looked
deeply into the new features of AC2K+ let alone Jet 4. This is a very
comprehensive list of the Jet 4 features and you are right, there are a
lot of great 'new' capabilities. I was particularly impressed with the
ability to create Procedures (although I suspect they are limited to
underlying querys and do not have the features that T-SQL has) and with
the ability to do bi-directional replication with SQL Server. I've added
that link to my ever growing list of Access resources. Thanks.

--
Bri

Mar 15 '06 #8
Bri

rkc wrote:
Bri wrote:

rkc wrote:

AUTOINCREMENT


Excellent! I searched through the Create Table help and it didn't have
specific info on Data Type. The Data Type help calles it AutoNumber
not AutoIncrement. Where did you find that AutoIncrement was the
correct Type? Sometimes help needs help.


I have no idea where I learned half of what I know. I dug that bit
of info out of a class that writes create table ddl to an html file.

A Google search now that you know should reveal several resources.

I just did a search and discovered that COUNTER also works.


Searching for AUTOINCREMENT in help found it in a comparison of data
type equivalents to ANSI SQL data types and in the list of reserved
words. Everywhere else in help it is referred to as Autonumber. I guess
this lack of consistancy is why it was so hard to find. None of the help
examples showed it's use either. In the link that Lyle supplied, it is
explained and is used in an example.

Thanks for the info.

--
Bri

Mar 15 '06 #9
When Jet 4.0 came out, we were told that:

"But to maintain backward compatibility with earlier versions of Microsoft
Jet, the enhancements to SQL are available only when the database engine is
set to operate in ANSI SQL 92 mode. "

As it turns out, either that was only partly true, or else DAO has since
been extended. I was told that I couldn't use the new ALTER TABLE stuff: it
was years later that someone else pointed out that I could.

(david)
"Lyle Fairfield" <ly***********@aim.com> wrote in message
news:11**********************@j33g2000cwa.googlegr oups.com...
TTBOMR, when MS-Access went from JET 3.5 to JET 4.0, it was also the
time when ADPs and MSDE were at the fore-front of the glitz message
from MS. Either they forgot to tell us about JET 4.0, or many of us,
including me, forgot to look.
The !!!!!new!!!!! (6 years old?) thingmes in JET 4.0 are discussed at
http://support.microsoft.com/kb/q275561/
but not as extensively as one might wish.
JET 4.0 has some great !!!!!new!!!!! capabilities, but as I see it,
they are 99.44% ignored in CDMA and elsewhere.

May 4 '06 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
2821
by: Jan Hendrickx | last post by:
Hi all, I use (something like) following code to add new records to a database, but it doesn't work!!! I need to know the value of an AutoNumber-field when I add the record. Why doesn't it...
1
5247
by: Mike Wiseley | last post by:
You can create a query in the SQL view that adds a new column to a table. For example, to add a new text field named X10, you use the following language: Alter Table MyTargetTable1 Add Column...
3
7871
by: ben.werdmuller | last post by:
Hi, Is there an easy way in ASP/VBscript to grab an autonumber (primary key) field just after an SQL insert? This is probably easy, but I'm stuck .. Cheers.
1
2408
by: S. van Beek | last post by:
Dear reader, By append a new record to a table I always expect the next higher value in the range of the autonumber field. But sometimes if I have deleted same records from a table the...
4
1413
by: Dan Keeley | last post by:
Hi. I'm inserting a record into a DB, how do i get the value back of an AutoNumber field so i can use it in subsequent SQL statements? At the moment I have code vaguely like this: Dim...
6
6220
by: HS1 | last post by:
Hello I have a table in Access Database. This table has a AutoNumber field. I use a DataGrid to show that table When I insert a new record in for this table using a DataGrid, there is a...
1
3934
by: gtwannabe | last post by:
I'm having a problem with a form that uses AutoNumber as the primary key. I have an Abort button to delete the current record and close the form. If AutoNumber is assigned, the code executes a...
2
2036
by: Richard Sherratt | last post by:
I've inherited a system that was designed by someone with no understanding of database design. From the logical design point of view, there is no logical design. The physical design is a nightmare....
4
2630
by: Amy Badgett | last post by:
I have figured out that there is no easy way to create an autonumber field in a query, but that there is a way (supposedly) to create an autonumber field in a table or rather, there is already an...
0
7193
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7067
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7264
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7316
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
7449
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5562
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
3160
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
1495
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
371
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.