i am using employee table to test if i am doing the whole program correct. I have 5 buttons. one is to add records and another to save(commit), to update, to delete and to clear. I am having trouble with the code to save the record to the data set. when i click add, it lets me add the new name but when i press save(commit), it wont save. i already added the data adapeter. this is my code:

Public Class Form1

Dim inc As Integer
Dim MaxRows As Integer
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 Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load

    dbProvider = "Provider = Microsoft.ACE.OLEDB.12.0;"
    dbSource = "Data Source = C:\Users\lyndalocks\Desktop\TESTPROJECT1.accdb; Persist Security Info=false;"

    con.ConnectionString = dbProvider & dbSource

    con.Open()
    sql = "SELECT * FROM [Employees]"
    da = New OleDb.OleDbDataAdapter(sql, con)
    da.Fill(ds, "TEST PROJECT 1")


    'MsgBox("Database is now open")

    con.Close()
    'MsgBox("Database is now closed")

    MaxRows = ds.Tables("TEST PROJECT 1").Rows.Count
    inc = -1




End Sub

Private Sub NavigateRecords()
    txtFirstName.Text = ds.Tables("TEST PROJECT 1").Rows(inc).Item(1)
    txtSurname.Text = ds.Tables("TEST PROJECT 1").Rows(inc).Item(2)
End Sub

Private Sub Button3_Click(sender As Object, e As EventArgs) Handles btnNext.Click
    If inc <> MaxRows - 1 Then

        inc = inc + 1

        NavigateRecords()

    Else

        MsgBox("No More Rows")

    End If
End Sub

Private Sub Button2_Click(sender As Object, e As EventArgs) Handles btnPrevious.Click
    If inc > 0 Then

        inc = inc - 1

        NavigateRecords()

    Else

        MsgBox("First Record")

    End If
End Sub

Private Sub Button4_Click(sender As Object, e As EventArgs) Handles btnLast.Click
    If inc <> MaxRows - 1 Then

        inc = MaxRows - 1

        NavigateRecords()

    End If
End Sub

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles btnfirst.Click
    If inc <> 0 Then

        inc = 0

        NavigateRecords()

    End If
End Sub

Private Sub btnUpdate_Click(sender As Object, e As EventArgs) Handles btnUpdate.Click
    Dim cb As New OleDb.OleDbCommandBuilder(da)


    ds.Tables("Test Project 1").Rows(inc).Item(1) = txtFirstName.Text
    ds.Tables("Test Project 1").Rows(inc).Item(2) = txtSurname.Text

    MsgBox("Data updated")

End Sub

Private Sub btnAddNew_Click(sender As Object, e As EventArgs) Handles btnAddNew.Click
    btnCommit.Enabled = True
    btnAddNew.Enabled = False
    btnUpdate.Enabled = False
    btnDelete.Enabled = False

    txtFirstName.Clear()
    txtSurname.Clear()
End Sub

Private Sub btnClear_Click(sender As Object, e As EventArgs) Handles btnClear.Click
    btnCommit.Enabled = False
    btnAddNew.Enabled = True
    btnUpdate.Enabled = True
    btnDelete.Enabled = True

    inc = 0
    NavigateRecords()
End Sub

Private Sub btnCommit_Click(sender As Object, e As EventArgs) Handles btnCommit.Click

    If inc <> -1 Then

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


        dsNewRow = ds.Tables("Test Project 1").NewRow()

        dsNewRow.Item("FirstName") = txtFirstName.Text
        dsNewRow.Item("LastName") = txtSurname.Text

        ds.Tables("Test Project 1").Rows.Add(dsNewRow)

        da.Update(ds, "Test Project 1")

        MsgBox("New Record added to the Database")

        btnCommit.Enabled = False
        btnAddNew.Enabled = True
        btnUpdate.Enabled = True >
        btnDelete.Enabled = True



    End If

End Sub

Dani AI

Generated

Quick checklist of the high‑value problems that will stop a new row from being written, and how to fix them quickly.

  1. Guard logic: the commit routine only runs when a flag based on inc is true. That check is inverted for "add new" mode, so the add/commit code path is skipped. Use a dedicated boolean (for example isAdding) or change the condition so the commit handler always runs when the UI is in add mode.
  2. Table-name/typo risk: you use a literal table name in several places. One small typo or different capitalization (e.g. "TEST PROJECT 1" vs "Test Project 1") will make ds.Tables(...) fail. Store the name once in a variable and reference that variable everywhere.
  3. Commands for the DataAdapter: @G_Waddell is correct — Update/Insert/Delete must exist for da.Update to work. OleDbCommandBuilder can auto-generate them, but only if the SELECT includes the primary key and the DataTable has that key defined. After Fill, set the DataTable.PrimaryKey to the identity column so the CommandBuilder can produce correct UPDATE/DELETE SQL.

Small, safe patterns to adopt (pseudo-VB shown so intent is clear):

Dim tbl As String = "Employees"
' after Fill:
ds.Tables(tbl).PrimaryKey = New DataColumn() { ds.Tables(tbl).Columns("EmployeeID") }
' on AddNew: set isAdding = True
' on Commit:
Try
  Dim cb As New OleDb.OleDbCommandBuilder(da)
  da.Update(ds, tbl)
Catch ex As Exception
  MessageBox.Show(ex.Message)
End Try

Also follow ’s advice and enable CLR exceptions / run under the debugger so you see any thrown exception text (wrap da.Update in Try/Catch if you’re not already). After a successful update, refresh your MaxRows and set inc to the new row index (or rebind the view) so navigation shows the newly saved record. For longer term reliability, consider using a BindingSource/DataTable binding instead of manual index arithmetic — it removes many of the navigation/update edge cases.

Recommended Answers

All 2 Replies

Maybe there's error happening, try this. On Visual Studio enable Common Language Runtime Exception, hit CTRL + ALT + E and Check Common Language Runtime Exception then OK. Run again your program and see if there's error.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.