-1

When I try cmd.ExecuteNonQuery() I get an error saying "Syntax error in INSERT INTO statement."

I posted this same problem yesterday... can someone help me again?

Private Sub btnadd1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnadd1.Click

    Dim cmd As New OleDb.OleDbCommand
    Dim Printlist1 As New DataTable
    If Not con.State = ConnectionState.Open Then
        con.ConnectionString = "Provider=Microsoft.Ace.OLEDB.12.0; Data Source=c:Database11.accdb"
        con.Open()
        cmd.Connection = con
    End If

    If Me.text1.Tag & "" = "" Then
        cmd.CommandText = "INSERT INTO Printlist1(StickerCode, Description, Company, Department, Location, User, SerialNumber, DatePurchased, Tagable, Quantity, Brand, Model)" & _
                    " VALUES(@StickerCode, @Description, @Company, @Department, @Location, @User, @SerialNumber, @DatePurchased, @Tagable, @Quantity, @Brand, @Model)"

        cmd.Parameters.AddWithValue("@StickerCode", Me.text1.Text)
        cmd.Parameters.AddWithValue("@Description", Me.text2.Text)
        cmd.Parameters.AddWithValue("@Company", Me.text3.Text)
        cmd.Parameters.AddWithValue("@Department", Me.text4.Text)
        cmd.Parameters.AddWithValue("@Location", Me.text5.Text)
        cmd.Parameters.AddWithValue("@User", Me.text6.Text)
        cmd.Parameters.AddWithValue("@SerialNumber", Me.text7.Text)
        cmd.Parameters.AddWithValue("@DatePurchased", Me.text8.Text)
        cmd.Parameters.AddWithValue("@Tagable", Me.text9.Text)
        cmd.Parameters.AddWithValue("@Quantity", Me.text10.Text)
        cmd.Parameters.AddWithValue("@Brand", Me.text11.Text)
        cmd.Parameters.AddWithValue("@Model", Me.text12.Text)

        cmd = New OleDbCommand(cmd.CommandText, con)
        cmd.ExecuteNonQuery()
    Else


        cmd.CommandText = "UPDATE Printlist1 " & _
                    " SET StickerCode='" & Me.text1.Text & _
                    ", Description='" & Me.text2.Text & "'" & _
                    ", Company='" & Me.text3.Text & "'" & _
                    ", Department='" & Me.text4.Text & "'" & _
                    ", Location='" & Me.text5.Text & "'" & _
                    ", User='" & Me.text6.Text & "'" & _
                    ", SerialNumber='" & Me.text7.Text & "'" & _
                    ", DatePurchased='" & Me.text8.Text & "'" & _
                    ", Tagable='" & Me.text9.Text & "'" & _
                    ", Quantity='" & Me.text10.Text & "'" & _
                    ", Brand='" & Me.text11.Text & "'" & _
                    ", Model='" & Me.text12.Text & "'" & _
                    " WHERE text1=" & Me.text1.Tag


        cmd.ExecuteNonQuery()

    End If
    RefreshData()
    Me.btnclear1.PerformClick()
    con.Close()
End Sub

Sticker Code Description Company Department Location User Serial Number Date Purchased Tagable Quantity Brand Model

8
  • can you share table structure for Printlist1 Commented Dec 4, 2013 at 5:23
  • The else logic (update) should also use parameters, but has nothing to do with your current issue. What is the actual error? Go into the inner exception if you have to find one, and a stack trace is also useful. Commented Dec 4, 2013 at 5:23
  • @dbw i dont know how to share to you the table structure of Printlist1 Commented Dec 4, 2013 at 5:48
  • @ps2goat how to user the parameters ins else logic(update)? Commented Dec 4, 2013 at 5:50
  • @user3049808 just edit your question and place the table structure at end Commented Dec 4, 2013 at 6:00

1 Answer 1

3

User is a reserved word in Sql try placing it in Square Brackets like this [User]


        cmd.CommandText = "INSERT INTO Printlist1(StickerCode, [Description], Company, Department, Location, [User], SerialNumber, DatePurchased, Tagable, Quantity, Brand, Model)" & _
                " VALUES(@StickerCode, @Description, @Company, @Department, @Location, @User, @SerialNumber, @DatePurchased, @Tagable, @Quantity, @Brand, @Model)"
Sign up to request clarification or add additional context in comments.

2 Comments

it didnt work my friend
Description is also a reserved word.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.