Syntax error in INSERT INTO statement ?

qaisqais

Member
Joined
Mar 29, 2010
Messages
14
Programming Experience
Beginner
keep getting this error no matter what i do :
Syntax error in INSERT INTO statement.

using the exact same piece of code in other forms and it's working fine..anyone help me solve this please? it's 5:20 am and this school project is driving me insane :mad:

VB.NET:
Public Class CreateProduct
    Dim con As New OleDb.OleDbConnection
    Dim dbProvider As String
    Dim dbSource As String
    Dim ds As New DataSet
    Dim da As OleDb.OleDbDataAdapter
    Dim sql As String
 Private Sub CreateProduct_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        dbProvider = "PROVIDER=Microsoft.Jet.OLEDB.4.0;"
        dbSource = "Data Source = C:\Documents and Settings\Administrator\My Documents\Visual Studio 2010\Projects\mySystemRemake\SystemDb.mdb"
        con.ConnectionString = dbProvider & dbSource
    End Sub
  Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Select Case MsgBox("Add this Product to Database?", MsgBoxStyle.YesNo, "Add Product")
            Case MsgBoxResult.Yes
                If TextBox1.Text = "" Or TextBox2.Text = "" Or TextBox3.Text = "" Or TextBox4.Text = "" Or RichTextBox1.Text = "" Then
                    MsgBox("Please ensure all fields are completed", 0, "Input Error")
                Else : con.Open()
                    sql = "SELECT * FROM ProductTbl"
                    da = New OleDb.OleDbDataAdapter(sql, con)
                    da.Fill(ds, "ProductEntry")
                    Dim cb As New OleDb.OleDbCommandBuilder(da)
                    Dim newentry As DataRow
                    newentry = ds.Tables("ProductEntry").NewRow()
                    newentry.Item("ProductName") = TextBox1.Text
                    newentry.Item("Product Information") = RichTextBox1.Text
                    newentry.Item("Product Quantity") = CInt(TextBox4.Text)
                    newentry.Item("Product Price") = CInt(TextBox3.Text)
                    newentry.Item("Type") = TextBox2.Text
                    ds.Tables("ProductEntry").Rows.Add(newentry)
                    da.Update(ds, "ProductEntry")
                    ds.Clear()
                    MsgBox("Product succesfully added to Database", 0, "")
                    con.Close()
                End If
            Case MsgBoxResult.No
                Me.Close()
        End Select
    End Sub
End Class
 
This error usually occurs because one or more of your column names contains reserved words or special characters. In that case, you have three options:

1. Change the column names in the database to avoid reserved words and special characters. This should be your first choice.

2. Set the QuotePrefix and QuoteSuffix properties of the command builder to escape column names. For an Access database you can use "[" and "]" or I think "`" will work too.

3. Don't use a command builder. Create the DeleteCommand, InsertCommand and UpdateCommand for the data adapter yourself.
 
AHA! {Slaps hand to forehead - sheesh!}

Thank you - not my post - not a school project - but sometimes ya just have to get nudged in the right direction. Very nice reply, by the way. Seems like a good place to be - even for an old timer just getting to grips with VB.NET.

-Roy
 
Back
Top