By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,528 Members | 1,338 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,528 IT Pros & Developers. It's quick & easy.

Problems With Insert Query with Auto Number field

P: 8
Hi All,

I'm trying to insert record to Access DB to a tbale that contains
its first field as AutoNumber field using the following command:

Expand|Select|Wrap|Line Numbers
  1. "INSERT INTO Threads VALUES (" + category + "')";
And it gives me the following error: "Number of query values and destination fields are not the same".

As far as I'm understand I don't need to add the first field because it's AutoNumber, So why this error is always appearing?

Please Help,
10x
Feb 25 '08 #1
Share this Question
Share on Google+
1 Reply


Expert Mod 2.5K+
P: 2,545
...
Expand|Select|Wrap|Line Numbers
  1. "INSERT INTO Threads VALUES (" + category + "')";
And it gives me the following error: "Number of query values and destination fields are not the same".
As far as I'm understand I don't need to add the first field because it's AutoNumber, So why this error is always appearing?...
Hi Liorjj. There are two forms of the INSERT statement: one in which you define the fields to be inserted and their insertion order, and the other in which you don't define the fields and instead rely on implied ordering. It is the implied order version you are using, where the SQL will match the first field in your table to the first item in the VALUES list, the second field to the second item and so on.

Because your autonumber field is the first one you can see that implied ordering is leaving you trying to overwrite this field, as you have not told it the actual name of the field you really want to insert.

You need to explicitly define the field name before your VALUES statement:
Expand|Select|Wrap|Line Numbers
  1.  
  2. "INSERT INTO Threads FIELDS([FieldName]) VALUES (" + category + "')";
  3.  
By the way, you seem to have an extra " ' " character at the end of your statement, before the closing bracket: " ') "

Regards

Stewart
Feb 25 '08 #2

Post your reply

Sign in to post your reply or Sign up for a free account.