Syntax error in INSERT INTO statement

Versaiteis

Member
Joined
Mar 12, 2010
Messages
14
Programming Experience
1-3
Alright, I admit that I have a similar post in the MS Access section, but I feel that the problem that I am having relies between the data adapter and the command builder =\

da=data adapter
ds=dataset

VB.NET:
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        con.ConnectionString = "PROVIDER=Microsoft.ACE.OLEDB.12.0;Data Source = Single.mdb"

        con.Open()
        sql = "SELECT * FROM tblClients"
        da = New OleDb.OleDbDataAdapter(sql, con)
        da.Fill(ds, "AddressBook")
        con.Close()

Private Sub btnCommit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCommit.Click
        If inc <> -1 Then
            Dim cb As New OleDb.OleDbCommandBuilder(da)
            Dim dsNewRow As DataRow
    
            MsgBox(cb.GetInsertCommand.CommandText)
            da.InsertCommand = cb.GetInsertCommand
            da.UpdateCommand = cb.GetUpdateCommand
            da.DeleteCommand = cb.GetDeleteCommand

            dsNewRow = ds.Tables("AddressBook").NewRow()

            dsNewRow.Item(0) = 3
            dsNewRow.Item(1) = txtFirstName.Text
            dsNewRow.Item(2) = Date.Now

            ds.Tables("AddressBook").Rows.Add(dsNewRow)

            da.Update(ds, "AddressBook")

            MsgBox("New Record added to Database")

            btnCommit.Enabled = False
            btnAddNew.Enabled = True
            btnUpdate.Enabled = True
            btnDelete.Enabled = True
        End If
    End Sub

The Insert command that the message box displays looks fine, I can see no syntax errors in it, and yet the program crashes on the update line
VB.NET:
da.update(ds, "AddressBook")

I will be more than happy to provide any other needed information, I am quite at a loss for what to do next. This problem has been a thorn in my side for a while -_-
 
Resolved

The solution was found.

Where this was my SQL statement:

VB.NET:
INSERT INTO tblClients (First Name, Last Name) VALUES (?,?)

The correct statement was:

VB.NET:
INSERT INTO tblClients (First_Name, Last_Name) VALUES (?,?)
 
Just an FYI...

Pretty sure you could also have just enclosed the field names in brackets.

VB.NET:
INSERT INTO tblClients ([First Name], [Last Name]) VALUES (?,?)

That should force them to be recognized as field names.
 
Back
Top