Connecting Tech Pros Worldwide Help | Site Map

Incorrect Syntax in INSERT INTO STATEMENT

Newbie
 
Join Date: Sep 2009
Posts: 3
#1: Sep 26 '09
Hi, I have a ploblem with an insert statement using an access database, I have the same code with a sql database which works but it doesn't seem to work on access. Could someone please help.
Expand|Select|Wrap|Line Numbers
  1.  For i = 0 To dg.Rows.Count - 1
  2.             Main.Command.CommandText = "INSERT INTO Diary (Date1, Time, Appointment) VALUES (?, ?, ?)"
  3.             Dim param1 As System.Data.OleDb.OleDbParameter = New System.Data.OleDb.OleDbParameter("Date1", dg(1, i).Value)
  4.             Dim param2 As System.Data.OleDb.OleDbParameter = New System.Data.OleDb.OleDbParameter("Time", dg(2, i).Value)
  5.             Dim param3 As System.Data.OleDb.OleDbParameter = New System.Data.OleDb.OleDbParameter("Appointment", dg(3, i).Value)
  6.  
  7.             Main.Command.Parameters.Add(param1)
  8.             Main.Command.Parameters.Add(param2)
  9.             Main.Command.Parameters.Add(param3)
  10.  
  11.             Try
  12.                 Main.Command.ExecuteNonQuery()
  13.             Catch ex As Exception
  14.                 MsgBox(ex.Message)
  15.             End Try
  16.             Main.Command.Parameters.Clear()
  17.  
i get "syntax error in INSERT INTO Statement" i've tried writing the values directly into the command text and that gives me the same error. Am I missing something totally obvious here?

Thanks in advance.
Plater's Avatar
Moderator
 
Join Date: Apr 2007
Location: New England
Posts: 7,148
#2: Sep 28 '09

re: Incorrect Syntax in INSERT INTO STATEMENT


Access uses slightly different forms of SQL, have you verified that your SQL code will work in access before trying to use it in .NET?

Is this "(?, ?, ?)" a valid way to add parameters?
Reply