views:

148

answers:

2

A trying to insert a couple of values received from textboxes into a table in access database. I have perfectly written my statement without passing value for the primary key in the table since its datatype is set to auto number.

Upon running the program. i click the button that runs the insert statement and i recieve the following error "NUMBER OF QUERY VALUES AND DESTINATION FIELDS ARE NOT THE SAME"

How can i fix this...?

please let me know if any further information is required.... Thanks...

+2  A: 

Have you listed the columns you're inserting into?

INSERT INTO table (col1, col2) VALUES (val1, val2)

You must do this if you are not inserting into every column, and should do it anyway as it's best practice.

David M
sqlQuery = "INSERT INTO Youth (" + "NumbersOfSport, " + "YouthID, " + "Price, " + "TotalCostOfTraining, " + "PercentageDiscount, " + "AmountDue" + "DatePurchased" + ") VALUES (" + toSql(qtyInt) + ", " + toSql(youthInt) + ", " + toSql(priceStr) + ", " + toSql(totalCstStr) + ", " + toSql(discountStr) + ", " + toSql(amtDueStr) + ", " + toSql(Convert.ToDateTime(purDate)) + ")";Above is exactly what the code looks like without values for the primarykey section How do i go about it
Selase
You're missing the comma between AmountDue and DatePurchased. It thinks you only have six columns.
David M
Thank you...very much for the sharp eyes...
Selase
i inserted the comma and i get another error that says "syntax error (missing operator) in query expression "19/12/2009 4:08:02"". am passing the current date and time to the datepurchased column and its datatype in access is also set to date/time. which operator am i missing?
Selase
You need delimiters around a date/time. If my recollection of access is right, you might be able to surround the date/time string with # characters to make it a date time literal?
David M
+2  A: 

You should post your query with a question like this, but i think i know what your problem is.

Since you're not using the primary key column and therefore inserting into only a subset of the fields, you need to list the fields you will be using like this:

INSERT INTO SomeTable (col1,col2,col3) VALUES ('val1','val2',3)

You were probably trying to do something like this:

INSERT INTO SomeTable VALUES ('val1','val2',3)

Which will not work because your primary key field is auto numbered.

Try this:

sqlQuery = 
"INSERT INTO Youth (" + 
"NumbersOfSport, " + 
"YouthID, " + 
"Price, " + 
"TotalCostOfTraining, " + 
"PercentageDiscount, " + 
"AmountDue," + 
"DatePurchased" + 
") VALUES (" + 
toSql(qtyInt) + ", " + 
toSql(youthInt) + ", " + 
toSql(priceStr) + ", " + 
toSql(totalCstStr) + ", " + 
toSql(discountStr) + ", " + 
toSql(amtDueStr) + ", " + 
toSql(Convert.ToDateTime(purDate)) + ")"

You were missing a comma here: "AmountDue" + should be: "AmountDue," +

VB format:

sqlQuery = "INSERT INTO Youth (" + "NumbersOfSport, " + "YouthID, " + "Price, " + "TotalCostOfTraining, " + "PercentageDiscount, " + "AmountDue" + "DatePurchased" + ") VALUES (" + toSql(qtyInt) + ", " + toSql(youthInt) + ", " + toSql(priceStr) + ", " + toSql(totalCstStr) + ", " + toSql(discountStr) + ", " + toSql(amtDueStr) + ", " + toSql(Convert.ToDateTime(purDate)) + ")"

Paul Sasik
sqlQuery = "INSERT INTO Youth (" + "NumbersOfSport, " + "YouthID, " + "Price, " + "TotalCostOfTraining, " + "PercentageDiscount, " + "AmountDue" + "DatePurchased" + ") VALUES (" + toSql(qtyInt) + ", " + toSql(youthInt) + ", " + toSql(priceStr) + ", " + toSql(totalCstStr) + ", " + toSql(discountStr) + ", " + toSql(amtDueStr) + ", " + toSql(Convert.ToDateTime(purDate)) + ")";Above is my insert statement as in the program code
Selase
that is without the primary key values. so how do i edit that section pertaining to what you have advised..?
Selase
See comment below.
David M
i inserted the comma and i get another error that says "syntax error (missing operator) in query expression "19/12/2009 4:08:02"".am passing the current date and time to the datepurchased column and its datatype in access is also set to date/time. which operator am i missing?
Selase
Try to format your datetime as ODBC canonical like this: yyyy-mm-dd hh:mm:ss:sss so your example: 2009-12-19 04:08:02.000
Paul Sasik
Also: we can't really tell exactly what your toSql method will do with the DateTimebut you could also try something like this:toSql(purDate.ToString) Then again, we don't really know what type or format purDate is...
Paul Sasik