0

Im doing a to-do list program, the error is when I click save, then it will show me the error and here is the full code, below will have the code that error message pops up

Public Class frmForm1

Dim inc As Integer = 0
Dim MaxRows As Integer

Dim con As New OleDb.OleDbConnection
Dim ds As New DataSet
Dim da As New OleDb.OleDbDataAdapter
Dim sql As String
Private Sub frmNavigate_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    con.ConnectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=F:\project.accdb;"
    con.Open()
    sql = "SELECT * from todolist"
    da = New OleDb.OleDbDataAdapter(sql, con)
    da.Fill(ds, "todolist")

    con.Close()

    MaxRows = ds.Tables("todolist").Rows.Count

    txtitem.Text = ds.Tables("todolist").Rows(inc).Item(1)
    txtdescript.Text = ds.Tables("todolist").Rows(inc).Item(2)
    dtptoday.Text = ds.Tables("todolist").Rows(inc).Item(3)
    dtpcomplete.Text = ds.Tables("todolist").Rows(inc).Item(4)

End Sub
Private Sub NavigateRecords()

    txtitem.Text = ds.Tables("todolist").Rows(inc).Item(1)
    txtdescript.Text = ds.Tables("todolist").Rows(inc).Item(2)
    dtptoday.Text = ds.Tables("todolist").Rows(inc).Item(3)
    dtpcomplete.Text = ds.Tables("todolist").Rows(inc).Item(4)

End Sub

Private Sub btnNext_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnNext.Click
    If inc <> MaxRows - 1 Then
        inc = inc + 1
        NavigateRecords()
    Else
        MsgBox("No More Rows")
    End If

End Sub

Private Sub btnPrevious_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnPrevious.Click
    If inc > 0 Then
        inc = inc - 1
        NavigateRecords()
    ElseIf inc = -1 Then
        MsgBox("No Records Yet")
    ElseIf inc = 0 Then
        MsgBox("First Record")
    End If
End Sub
Private Sub btnLast_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnLast.Click
    If inc <> MaxRows - 1 Then
        inc = MaxRows - 1
        NavigateRecords()
    End If

End Sub
Private Sub btnFirst_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnFirst.Click
    If inc <> 0 Then
        inc = 0
        NavigateRecords()
    End If

End Sub

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnUpdate.Click
    ds.Tables("todolist").Rows(inc).Item(1) = txtitem.Text
    ds.Tables("todolist").Rows(inc).Item(2) = txtdescript.Text
    ds.Tables("todolist").Rows(inc).Item(3) = dtptoday.Text
    ds.Tables("todolist").Rows(inc).Item(4) = dtpcomplete.Text


    da.Update(ds, "todolist")

    MsgBox("Data updated!")

End Sub

Private Sub btnadd_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnadd.Click
    btnSave.Enabled = True
    btnadd.Enabled = False
    btnUpdate.Enabled = False
    btnremove.Enabled = False


    txtitem.Clear()
    txtdescript.Clear()


End Sub

Private Sub btncancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btncancel.Click
    btnSave.Enabled = False
    btnadd.Enabled = True
    btnUpdate.Enabled = True
    btnremove.Enabled = True

    inc = 0
    NavigateRecords()
End Sub

Private Sub btnSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSave.Click
    If inc <> -1 Then
        Dim cb As New OleDb.OleDbCommandBuilder(da)
        Dim dsNewRow As DataRow

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

        dsNewRow.Item("Task") = txtitem.Text
        dsNewRow.Item("description") = txtdescript.Text
        dsNewRow.Item("Date of create") = dtptoday.Text
        dsNewRow.Item("Date of execute") = dtpcomplete.Text

        ds.Tables("todolist").Rows.Add(dsNewRow)
        MaxRows = MaxRows + 1
        da.Update(ds, "todolist")
        MsgBox("New Record added to the Database!")
        btnSave.Enabled = False
        btnadd.Enabled = True
        btnUpdate.Enabled = True
        btnremove.Enabled = True

        inc = 0
        NavigateRecords()

    End If

End Sub

Private Sub btnremove_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnremove.Click
    If MessageBox.Show("Do you really want to Delete this Record?", _
        "Delete", MessageBoxButtons.YesNo, _
        MessageBoxIcon.Warning) = DialogResult.No Then

        MsgBox("Operation Cancelled")
        Exit Sub
    End If

    Dim cb As New OleDb.OleDbCommandBuilder(da)

    ds.Tables("todolist").Rows(inc).Delete()
    MaxRows = MaxRows - 1

    inc = 0
    NavigateRecords()
    da.Update(ds, "todolist")

End Sub


Private Sub btnsummary_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnsummary.Click

    Me.Hide()
    summary.Show()
End Sub
End Class

The error is at

    Private Sub btnSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSave.Click
    If inc <> -1 Then
        Dim cb As New OleDb.OleDbCommandBuilder(da)
        Dim dsNewRow As DataRow

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

        dsNewRow.Item("Task") = txtitem.Text
        dsNewRow.Item("description") = txtdescript.Text
        dsNewRow.Item("Date of create") = dtptoday.Text
        dsNewRow.Item("Date of execute") = dtpcomplete.Text

        ds.Tables("todolist").Rows.Add(dsNewRow)
        MaxRows = MaxRows + 1
        da.Update(ds, "todolist") 'ERROR!!!!!
        MsgBox("New Record added to the Database!")
        btnSave.Enabled = False
        btnadd.Enabled = True
        btnUpdate.Enabled = True
        btnremove.Enabled = True

        inc = 0
        NavigateRecords()

    End If

End Sub

This line

da.Update(ds, "todolist")

I have solved it by adding this

Dim cb As New OleDb.OleDbCommandBuilder(da)
        cb.QuotePrefix = "["
        cb.QuoteSuffix = "]"
2
  • im wondering :\ when i run the program it show me the line oledbexception was unhandled @thetimmer Apr 23, 2014 at 15:22
  • is the database open? I had a table locking issue when I had edited a query in Access once and left it up unsaved. I see your credentials and realize you probably thought to check this already, just throwing it out there.
    – thetimmer
    Apr 23, 2014 at 15:32

1 Answer 1

1

In your Sub frmNavigate_Load(), you closed the connection. To fix this problem, you could either 1) leave your connection open, or 2) open it again each time you intend to perform a data operation (recommended).

Private Sub btnSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSave.Click
    If inc <> -1 Then
        Dim cb As New OleDb.OleDbCommandBuilder(da)
        Dim dsNewRow As DataRow

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

        dsNewRow.Item("Task") = txtitem.Text
        dsNewRow.Item("description") = txtdescript.Text
        dsNewRow.Item("Date of create") = dtptoday.Text
        dsNewRow.Item("Date of execute") = dtpcomplete.Text

        ds.Tables("todolist").Rows.Add(dsNewRow)
        MaxRows = MaxRows + 1
        'reopen a connection for your da
        con.open()
        'now you are connected, you can build an INSERT command and then save
        cb.GetInsertCommand()
        da.Update(ds, "todolist")
        'close it again
        con.close()
        MsgBox("New Record added to the Database!")
        btnSave.Enabled = False
        btnadd.Enabled = True
        btnUpdate.Enabled = True
        btnremove.Enabled = True

        inc = 0
        NavigateRecords()

    End If

End Sub
3
  • {"Syntax error in INSERT INTO statement."} it this to me @TimG Apr 23, 2014 at 17:39
  • You must not have used the CommandBuilder to create your INSERT statements yet. I added a line of code to do that. btw, you will have to add similar calls for UPDATE and DELETE (in your Subs).
    – TimG
    Apr 23, 2014 at 17:53
  • 1
    I have solved the problem by adding below the commandbuilder, thank for your answer btw cb.QuotePrefix = "[" cb.QuoteSuffix = "]" Apr 23, 2014 at 18:04

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.