Solved

Syntax for Inserting a record with an autonumber field

Posted on 2004-09-06
7
352 Views
Last Modified: 2008-03-06
I am trying to insert a record into a table that has four fields and I do not know the syntax for inserting a record with the an autonumbered field.

Table name - Autotest
Field 1 - id,  Number data type
FIeld 2 - name, Text data type
Field 3 - id2, Autonumber data type
Field 4 - description, Text data type

The SQL statement I am trying to run is:

INSERT INTO autotest
VALUES (1, 'chon',   , 'female');

I've tried ## in the space for the autonumber and several others but I get a data type mismathc when running it.

Please advise on the proper SQL syntax for MS Access.

Thanks,
Chon
 
0
Comment
Question by:dlabraham
7 Comments
 
LVL 41

Accepted Solution

by:
shanesuebsahakarn earned 50 total points
ID: 11990545
Specify which fields you want to append your data to:

INSERT INTO autotest (Field1, Field2, Field4) VALUES (1,'chon','female')
0
 
LVL 32

Expert Comment

by:jadedata
ID: 11990551
Greetings dlabraham!

INSERT INTO autotest VALUES (clng(1), 'chon',   , 'female');

since autonumbers are long data types, try specifying the datatype.

regards
:)-j-
0
 
LVL 8

Expert Comment

by:JonoBB
ID: 11990608
You cant tell an autonumber field what value you want to put in it, hence the name 'autonumber'

If the first field is an autonumber field, then you dont reference that field.
Try:

INSERT INTO Autotest (name, ID2, description) VALUES ('chon',   , 'female')
0
Free Gift Card with Acronis Backup Purchase!

Backup any data in any location: local and remote systems, physical and virtual servers, private and public clouds, Macs and PCs, tablets and mobile devices, & more! For limited time only, buy any Acronis backup products and get a FREE Amazon/Best Buy gift card worth up to $200!

 
LVL 8

Expert Comment

by:JonoBB
ID: 11990613
Oh, and this assumes that your ID2 field can accept null values - make sure that your field definition has been set accordingly, otherwise access will spit the dummy
0
 
LVL 84
ID: 11991039
>> You cant tell an autonumber field what value you want to put in it, hence the name 'autonumber'

Yes you can insert a Long Integer value into an AutoNumber field ... although there's no reason to do so.
0
 
LVL 44

Expert Comment

by:GRayL
ID: 11992547
As Shane implied above, do not reference the autonumer field.

INSERT INTO autotest (Field1, Field2, Field4) VALUES (1,'chon','female').

When the record is added, the autonumber datatype will create the value for that field.
0
 
LVL 44

Expert Comment

by:GRayL
ID: 11992565
Wished I could Spell!
0

Featured Post

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

In the previous article, Using a Critera Form to Filter Records (http://www.experts-exchange.com/A_6069.html), the form was basically a data container storing user input, which queries and other database objects could read. The form had to remain op…
Experts-Exchange is a great place to come for help with solutions for your database issues, and many problems are resolved within minutes of being posted.  Others take a little more time and effort and often providing a sample database is very helpf…
Using Microsoft Access, learn some simple rules for how to construct tables in a relational database. Split up all multi-value fields into single values: Split up fields that belong to other things into separate tables: Make sure that all record…
With Microsoft Access, learn how to start a database in different ways and produce different start-up actions allowing you to use a single database to perform multiple tasks. Specify a start-up form through options: Specify an Autoexec macro: Us…

757 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now