473,387 Members | 1,863 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

INSERT INTO with AutoNumber

Lyn
Hi,
Is there an "easy" way to write a full record (all fields) using "INSERT
INTO..." into a table which has an AutoNumber field?

Normally, to write a full new record I would use:

INSERT INTO [tblTable] VALUES (Value1, Value2, ...);

If (say) the first field (Value1) is an AutoNumber, what value can you put
that will preserve the auto-increment function?

NULL doesn't work because the field is a Long.

Omitting the field (via a comma placemarker) doesn't work either.

0 (zero) works in that the record will be written, but the AutoNumber field
will be set to 0 (contrary to what I have read that there is no way to
override an AutoNumber field!).

What does work is to specify all of the field names (except the AutoNumber
field -- leaving it out altogether allows the auto-increment function to
work normally). For example (Field1 is AutoNumber):

INSERT INTO [tblTable] (Field2, Field3,...) VALUES (Value2, Value3,...);

This is OK (and it is what I have done), but if you have a record with a
lot of fields, it means a lot of extra typing (and chances for errors) to
enter all of the field names except for the one which auto-increments. Is
there a simpler way to do this? (Such as a keyword "value" that says
"ignore me")

Or perhaps you think that it is good practice to list the field names in
the syntax anyway (even when there is no AutoNumber field and regardless of
how many fields there are)?

Cheers,
Lyn.
May 16 '07 #1
4 38344
Lyn wrote:
Hi,
Is there an "easy" way to write a full record (all fields) using
"INSERT INTO..." into a table which has an AutoNumber field?
[snip]
What does work is to specify all of the field names (except the
AutoNumber field -- leaving it out altogether allows the
auto-increment function to work normally). [snip]
Listing all field names except the AutoNumber field is the only way to do what
you want.

--
Rick Brandt, Microsoft Access MVP
Email (as appropriate) to...
RBrandt at Hunter dot com
May 16 '07 #2
Lyn
On Wed, 16 May 2007 11:30:07 GMT, Rick Brandt wrote:
Lyn wrote:
>Hi,
Is there an "easy" way to write a full record (all fields) using
"INSERT INTO..." into a table which has an AutoNumber field?
[snip]
>What does work is to specify all of the field names (except the
AutoNumber field -- leaving it out altogether allows the
auto-increment function to work normally). [snip]

Listing all field names except the AutoNumber field is the only way to do what
you want.
Thanks. I suspected as much :-(

Lyn.
May 16 '07 #3
Lyn wrote:
On Wed, 16 May 2007 11:30:07 GMT, Rick Brandt wrote:
>Lyn wrote:
>>Hi,
Is there an "easy" way to write a full record (all fields) using
"INSERT INTO..." into a table which has an AutoNumber field?
[snip]
>>What does work is to specify all of the field names (except the
AutoNumber field -- leaving it out altogether allows the
auto-increment function to work normally). [snip]

Listing all field names except the AutoNumber field is the only way
to do what you want.

Thanks. I suspected as much :-(

Lyn.
It is fairly easy to write a code routine that you can pass the name of a
table or query to and have it print all of the field names comma delimited
to the debug window where they can then easily be copied and pasted into
your query.

I created such a utility because I often have to work with tables on our
AS400 legacy system where the tables are definitely NOT normalized so having
a table with 50 or even 150 fields is not uncommon.

You can also build a simple SELECT query in the QBE grid and then switch to
SQL view and copy all the field names from there.

--
Rick Brandt, Microsoft Access MVP
Email (as appropriate) to...
RBrandt at Hunter dot com
May 16 '07 #4
Lyn
On Wed, 16 May 2007 13:20:19 GMT, Rick Brandt wrote:
>
It is fairly easy to write a code routine that you can pass the name of a
table or query to and have it print all of the field names comma delimited
to the debug window where they can then easily be copied and pasted into
your query.

I created such a utility because I often have to work with tables on our
AS400 legacy system where the tables are definitely NOT normalized so having
a table with 50 or even 150 fields is not uncommon.

You can also build a simple SELECT query in the QBE grid and then switch to
SQL view and copy all the field names from there.
Rick, thanks for the hint. Fortunately in my current case, the table had
only four fields so it was not a big deal. However, it occurred to me that
this could be a big problem if the table had a large number of fields.

Cheers,
Lyn.
May 16 '07 #5

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

Similar topics

3
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.
8
by: Carl | last post by:
Hi, I hope someone can share some of their professional advice and help me out with my embarissing problem concerning an Access INSERT query. I have never attempted to create a table with...
1
by: nicholas | last post by:
To insert a record in a Ms Access database and be able to retrieve the newly created ID (autonumber) I used the code below (code 1). Now, the problem is that this is not very secure and that, if...
1
by: loreille | last post by:
To insert a record in a Ms Access database and be able to retrieve the newly created ID (autonumber) I used the code below (code 1). Now, the problem is that this is not very secure and that, if...
8
by: petebeatty | last post by:
I have created a SQL string the properly inserts a record in the table. However, the insert does not occur at the end of the table. Instead it inserts a record after the last record that I viewed....
1
by: Jan | last post by:
I have a table with autoincrement unique ID plus name (required not to be blank) and other fields. I have a list of names in another table and would like to do insert to the name field of the...
10
by: MLH | last post by:
Suppose, in a multi-user environment, you have append query SQL in a VBA procedure that looks like INSERT INTO MyTable... and the next line reads MyVar=DMax("","MyTable... You can never be...
2
by: rn5a | last post by:
In a ASP applicatiuon, the FOrm has a textbox & a select list where the admin can select multiple options. Basically the admin has to enter the name of a new coach in the textbox & select the...
2
by: ramzansadiq | last post by:
Hi guru's, I m unable to find how to Insert a primary key which is also an autonumber field into Table. My query is as below.It is inserting record if i m hardcoding the ID Field. It is VBA Access...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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...

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.