I am trying to write a program where I can write data to an Access DB but I keep getting the error “Syntax error in INSERT INTO statement”.
FirstLast is the name of the table; First and Last are columns in it.
Imports System.Data.OleDb
Public Class Form1
Dim theConnectionString As New OleDbConnection
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
theConnectionString.ConnectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\Users\user\Documents\Database1.accdb"
Dim cmd As OleDbCommand = New OleDbCommand("INSERT INTO FirstLast (First, Last) VALUES (@First, TextBox1.text)", theConnectionString)
cmd.Parameters.Add("@First", OleDbType.Char, 255).Value = TextBox1.Text
cmd.Parameters.Add("@Last", OleDbType.Char, 255).Value = TextBox1.Text
Try
theConnectionString.Open()
cmd.ExecuteNonQuery()
Catch ex As Exception
Throw ex
Finally
theConnectionString.Close()
End Try
End Sub
End Class
@Firstand@Last— so shouldn’t you use those instead of?in the query?VALUES (@First, TextBox1.text)in your SQL, where it should sayVALUES (@First, @Last)? If so, I'm voting to close this question as "merely a typo".