Error when adding a record

ZeroEffect

Well-known member
Joined
Oct 24, 2004
Messages
180
Location
Detroit, MI
I am trying to add a record to my access database for the application I am building. I can connect and display data from the DB but when use my code to add a row I get this error. "Syntax error in INSERT INTO statement." My ID row is auto incrementing this might be the problem. The code I using is posted below. Thanks for your help.

ZeroEffect


Code:
    Private Sub btnAdd_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAdd.Click

        Dim cb As New OleDb.OleDbCommandBuilder(daEvents)
        Dim dsNewRow As DataRow

        dsNewRow = dsEvents.Tables("events").NewRow()

        dsNewRow.Item("ID") = vbNull This is Auto incrementing in Access, Possible issue here
        dsNewRow.Item("EventName") = tbEventName.Text
        dsNewRow.Item("Command") = tbCommand.Text
        dsNewRow.Item("Time") = dtpTime.Text
        dsNewRow.Item("Days") = strSu & strM & strT & strW & strR & strF & strSa

        dsEvents.Tables("events").Rows.Add(dsNewRow)

        daEvents.Update(dsEvents, "events")

        MsgBox("New Record added to the Database")

    End Sub
 
I tried this and I still get the same result. but with a little more research I found that my column labeled "Time" is a labeld with a reserved word. I changed it from "Time" to "RunTime" and everything worked. Here is a link to reserved works.

http://www.xlinesoft.com/asprunnerpro/articles/sql_access_odbc_reserved_keywords.htm

Thanks for your help. I am now working on getting the ID column to update in my DataGridView. It looks like all I have to do is reload the Data from the file.

And Now I have another issue. When I delete a row I get this error,

"Deleted row information cannot be accessed through the row."

This only happens when the row being deleted is NOT the last row.

Any thoughts on this?

Here is my Code for deleteing and where the error is happening.

Code:
    Private Sub btnDelete_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDelete.Click
        Dim cb As New OleDb.OleDbCommandBuilder(daEvents)
        dsEvents.Tables("events").Rows(DataGridView.CurrentRow.Index).Delete()
        dsEvents.AcceptChanges()
        daEvents.Update(dsEvents, "events")
        DataGridView.DataSource = dsEvents.Tables("events")

    End Sub

Here is where the debug stops

    Private Sub DataGridView_SelectionChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles DataGridView.SelectionChanged
        If blnFormClosing = False Then
            resetCB()
            tbEventName.Text = dsEvents.Tables("Events").Rows(DataGridView.CurrentRow.Index).Item("EventName")
            tbCommand.Text = dsEvents.Tables("Events").Rows(DataGridView.CurrentRow.Index).Item("Command")
            dtpTime.Text = dsEvents.Tables("Events").Rows(DataGridView.CurrentRow.Index).Item("RunTime")

            Days(dsEvents.Tables("Events").Rows(DataGridView.CurrentRow.Index).Item("Days"))

        End If
    End Sub

Thanks

ZeroEffect
 
Last edited by a moderator:
Back
Top