0
votes

How can I make this delete button work? Public Class Form1 Dim inc As Integer Dim MaxRows As Integer Dim dbProvider As String Dim dbSource As String Dim con As New OleDb.OleDbConnection Dim ds As New DataSet Dim da As OleDb.OleDbDataAdapter Dim sql As String

Private Sub GroupBox1_Enter(sender As System.Object, e As System.EventArgs) Handles GroupBox1.Enter

    dbProvider = "PROVIDER=Microsoft.Jet.OLEDB.4.0;"
    dbSource = "Data Source = C:Desktop\AddressBook\AddressBook.mdb"

    con.ConnectionString = dbProvider & dbSource

    con.Open() 

    sql = "SELECT * From Master_Details"
    da = New OleDb.OleDbDataAdapter(sql, con)
    da.Fill(ds, "Master_Details")

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

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

    txtcode.Text = ds.Tables("Master_Details").Rows(0).Item(0)
    txtdprt.Text = ds.Tables("Master_Details").Rows(0).Item(1)
    txtgroup.Text = ds.Tables("Master_Details").Rows(0).Item(2)
    txtepf.Text = ds.Tables("Master_Details").Rows(0).Item(3)
    txtdesignation.Text = ds.Tables("Master_Details").Rows(0).Item(4)
    txtename.Text = ds.Tables("Master_Details").Rows(0).Item(5)
    txtjdate.Text = ds.Tables("Master_Details").Rows(0).Item(6)
    txtdob.Text = ds.Tables("Master_Details").Rows(0).Item(7)
    txtnicn.Text = ds.Tables("Master_Details").Rows(0).Item(8)
    txtgender.Text = ds.Tables("Master_Details").Rows(0).Item(9)
    txttcodes.Text = ds.Tables("Master_Details").Rows(0).Item(10)
    Return
End Sub

Private Sub btndelete_Click(sender As System.Object, e As System.EventArgs) Handles btndelete.Click
    If MessageBox.Show("Do you really want to Delete this Record?", "Delete", MessageBoxButtons.YesNo, MessageBoxIcon.Warning) = DialogResult.No 
        MsgBox("Operation Cancelled")
        Exit Sub
    End If

    Dim cb As New OleDb.OleDbCommandBuilder(da)

    ds.Tables("Master_Details").Rows(ds.Tables("Master_Details").Rows.Count).Delete()
    MaxRows = MaxRows - 1
    inc = 0
    da.Update(ds, "Master_Details")
    NavigateRecords()
End Sub
2
work means, how to run this program without errors - user2089601

2 Answers

0
votes

You're not properly specifying which row to delete -- it's always row ds.Tables("Master_Details").Rows.Count, which is one past the last row.

0
votes

Use case statement... Put all of this in your delete button click event...

  Select Case MsgBox("Do you really want to delete this record?", MsgBoxStyle.Exclamation + MsgBoxStyle.YesNo, "CONFIRM DELETE")
        Case MsgBoxResult.Yes
            'NOT SURE IF THIS IS RIGHT, BUT THE SELECT IS IN WHICH YOU WANT...'
            Dim cb As New OleDb.OleDbCommandBuilder(da)
            ds.Tables("Master_Details").Rows(ds.Tables("Master_Details").Rows.Count - 1).Delete()
            MaxRows = MaxRows - 1
            inc = 0
            da.Update(ds, "Master_Details")
            NavigateRecords()
        Case MsgBoxResult.No
             MsgBox ("Operation Cancelled")
           Exit Sub
    End Select

Also set a break point on your first line if the messagebox result is yes and step through your code, this will show what is going on...

Thanks!