Click here to Skip to main content
15,917,618 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi
below is my code snapshot where i wanted to insert values in Inwards table, I have stores all variables like command,con in module. I am not getting any error but when i debugged then i got exception message as "Syntax error in INSERT INTO statement." plz help me,

VB
str = "Insert Into Inwards (SerialNumber,LetterNumber,DateOfEntry,Subject,From,LetterType,AssignedTo,BeltNumber,DateOfIssue) values(" + txtSerialNo.Text.Trim + "," + txtLetterNumber.Text.Trim + ",'" + lblDate.Text.Trim + "','" + txtSubject.Text.Trim + "','" + txtFrom.Text.Trim + "','" + cbType.Text.Trim + "','" + txtAssignedTo.Text.Trim + "','" + txtBeltNumber.Text.Trim + "','" + dtDOI.Text.Trim + "')"
               cmd = New OleDb.OleDbCommand(str, con)
               con.Open()
               cmd.ExecuteNonQuery()
               MsgBox("Inserted Successfully new Inward..")

               'data()
               'clearText()
           Catch ex As Exception

           Finally
               con.Close()

           End Try
Posted
Updated 5-Jun-13 0:07am
v2
Comments
Pheonyx 5-Jun-13 6:09am    
Question 1... What type are all of the columns? You appear to have some date columns but passing in strings.

If DateOfIssue field is date data type, please, use query like this:
SQL
INSERT INTO Table1 (DateField1)
VALUES(#2013/06/05#) 

for text:
SQL
INSERT INTO Table1 (TextField1)
VALUES('any text') 

for numeric values:
SQL
INSERT INTO Table1 (NumericField1)
VALUES(1000) 


Have a look here:
Add, edit, and delete data (MS Access)[^]
INSERT INTO Statement (Microsoft Access SQL)[^]
 
Share this answer
 
Comments
Shaikh Nadeem 5-Jun-13 6:25am    
I have taken all datafields as text only still showing same error on debugging.
Maciej Los 5-Jun-13 6:34am    
Do you want to tell that all fields in your table are text data type?
Shaikh Nadeem 5-Jun-13 6:42am    
yes,But thats not the problem
Maciej Los 5-Jun-13 6:54am    
OK, i got it.
Please use brackets: [] to define destination fields, especially: [FROM] ;)
Shaikh Nadeem 5-Jun-13 7:44am    
Still not working getting some other error as "The changes you requested to the table were not successful because they would create duplicate values in the index, primary key, or relationship. Change the data in the field or fields that contain duplicate data, remove the index, or redefine the index to permit duplicate entries and try again."
Google for "SQL Injection Attack" to see why what you're doing is so bad. It's a HUGE security risk.

Then Google for "VB.NET SQL Parameterized Queries" on how to fix it.

Yes, you can make an AutoNumber column in an Access database. If you made the database in Access it would have been staring you right in the face when you picked a field type for the column.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900