469,604 Members | 2,381 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,604 developers. It's quick & easy.

Problems With Insert Query with Auto Number field

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
1 1913
Stewart Ross
2,545 Expert Mod 2GB
...
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.

Similar topics

10 posts views Thread by johnnyboy10017 | last post: by
5 posts views Thread by me | last post: by
4 posts views Thread by so many sites so little time | last post: by
reply views Thread by guiromero | last post: by
reply views Thread by devrayhaan | last post: by
reply views Thread by gheharukoh7 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.