Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Shaun E on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Insert Into Syntax error (missing operator) in query expression

Status
Not open for further replies.

jgaylord

IS-IT--Management
Apr 17, 2006
11
US
Does anyone see what I am doing wrong?
I can Insert a single field.... But when I try and insert 2 fields I get this error. Syntax error (missing operator) in query expression I am trying to do this in VB 2005.


InsertCmd = New System.Data.OleDb.OleDbCommand("Insert Into Result (Ani, Xdate, Cdate, Xamount, Camount, Difference) Values ('" & txtAni.Text & "'," & txtXdate.Text & ",'" & txtCDate.Text & ",'" & txtXAmount.Text & ",'" & txtCAmount.Text & ",'" & txtDifference.Text & "')", dbconnection)
InsertCmd.ExecuteNonQuery()
 
Your apostrophes are all whacky.

Try...

Code:
InsertCmd = New System.Data.OleDb.OleDbCommand("Insert Into Result (Ani, Xdate, Cdate, Xamount, Camount, Difference) Values ('" & txtAni.Text & "','" & txtXdate.Text & "','" & txtCDate.Text & "','" & txtXAmount.Text & "','" & txtCAmount.Text & "','" & txtDifference.Text & "')", dbconnection)

Hint: When you have these problems, it's usually a good idea to temporarily display the SQL query so you can copy/paste it to Query Analyzer.

p.s. Look up SQL Injection and you should be using a command object to safeguard against the 'apostrophe problem'


-George

Strong and bitter words indicate a weak cause. - Fortune cookie wisdom
 
THANK YOU That worked perfectly! I dont understand what your talking about with the Query Analyzer... But I will take your advice and look it up. Thanks a Million!
 
Query Analyzer is an application that ships with SQL Server 2000. If you are using SQL Express or SQL Server 2005, then you would be using the SQL Server Management Studio (SSMS).

-George

Strong and bitter words indicate a weak cause. - Fortune cookie wisdom
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top