I found a tutorial online that so far has been aswesome. I downloaded the access db and so far can read and browse through it. However, when I try to write back to it (btnSubmit_Click), nothing happens. All other buttons work fine, I can still browse, and I get absolutely no feedback, buttons are not made available, and nothing is written to the db. After doing research all day, I switched the provider, retyped a few times, tried copy/pasting the code itself, and nada.
A little help?

Here 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(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

        dbProvider = "PROVIDER=Microsoft.ACE.OLEDB.12.0;"
        dbSource = "Data Source=C:\AddressBook.mdb"

        con.ConnectionString = dbProvider & dbSource

        con.Open()

        sql = "Select * From tblContacts"
        da = New OleDb.OleDbDataAdapter(sql, con)
        da.Fill(ds, "AddressBook")

        MsgBox("Database is open")



        con.Close()
        MsgBox("Connection is closed")

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

        btnCommit.Enabled = False


    End Sub

    Private Sub NavigateRecords()


        txtFirstName.Text = ds.Tables("AddressBook").Rows(inc).Item(1)
        txtSurname.Text = ds.Tables("AddressBook").Rows(inc).Item(2)
    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 btnUpdate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnUpdate.Click

        Dim cb As New OleDb.OleDbCommandBuilder(da)


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


        da.Update(ds, "AddressBook")

        MsgBox("Data updated")

    End Sub

    Private Sub btnAddNew_Click(ByVal sender As System.Object, ByVal e As System.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(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnClear.Click
        btnCommit.Enabled = False
        btnAddNew.Enabled = True
        btnUpdate.Enabled = True
        inc = 0
        NavigateRecords()

    End Sub

    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
            Try
                dsNewRow = ds.Tables("AddressBook").NewRow()

                dsNewRow.Item(1) = txtFirstName.Text
                dsNewRow.Item(2) = txtSurname.Text

                ds.Tables("AddressBook").Rows.Add(dsNewRow)
                da.Update(ds, "AddressBook")
                cb.GetUpdateCommand()

                MsgBox("New Record Added to Database")
            Catch ex As Exception
                MsgBox(ex.ToString)

            End Try
            btnCommit.Enabled = False
            btnAddNew.Enabled = True
            btnUpdate.Enabled = True
            btnDelete.Enabled = True

        End If


    End Sub
End Class

Recommended Answers

All 6 Replies

Hi I normally write my own command to do Updates Inserts etc then reload the data. But, I think you need to have the cb.GetUpdateCommand() line before the da.update

What error msg do you get?

Thanks, G Waddell, I moved it but the same thing happens; Nothing.
Savedlema, Nothing happens. I click 'commit', and the messagebox doesn't appear, the buttons dont enable, nothing. It doesn't hang, it just doesn't work...

OKAY I now have a new question. I troubleshooted the issue by putting messageboxes in, to see where it hung. Why I didn't think of that sooner is beyond me, but here:

   MsgBox("1")
        If inc <> -1 Then
            MsgBox("2")
            Dim cb As New OleDb.OleDbCommandBuilder(da)
            Dim dsNewRow As DataRow
            MsgBox("3")
            Try
                dsNewRow = ds.Tables("AddressBook").NewRow()
                MsgBox("4")
                dsNewRow.Item(1) = txtFirstName.Text
                dsNewRow.Item(2) = txtSurname.Text
                MsgBox("5")
                ds.Tables("AddressBook").Rows.Add(dsNewRow)
                MsgBox("6")
                cb.GetUpdateCommand()
                MsgBox("7")
                da.Update(ds, "AddressBook")
                MsgBox("8")

                MsgBox("New Record Added to Database")
            Catch ex As Exception
                MsgBox(ex.ToString)

            End Try
            btnCommit.Enabled = False
            btnAddNew.Enabled = True
            btnUpdate.Enabled = True
            btnDelete.Enabled = True

        End If

Anyway, it hung directly after MsgBox("1"). I removed the IF statement, and bam, it worked.
So, again, I'm a noob, but why'd that work?

Okay. I got it allllll figured out. I was paying attention to the wrong problem. I feel I should post the resolution since, after looking around online, all I found were "hey its not working" followed by "oh i figured it out" and nothing helpful posted.

So, to make it work is to simply hit the next button. The if statement says "If inc is greater or less than -1, then do this" However, when you open the program, inc IS -1 and therefore not work.
Now pardon me whilst I go slap myself silly for overlooking a thing like that.

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.