i trying to make a code to Delete and Update Access 2007 database after remove a selected item from a listbox in VB 2008. [using "Remove" button that i create]
pervious i already make a button to View data from database and Remove from listbox. but i don't know how to delete and update in database. hope someone can help me..

the code i using now:

Imports System.Data.OleDb

Public Class Form1
    Dim cn As OleDbConnection
    Dim cmd As OleDbCommand
    Dim cmd2 As OleDbCommand
    Dim dr As OleDbDataReader
    Dim str As String

    Private Sub btmView_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btmView.Click
        
        cn = New OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=Database11.mdb")

        cn.Open()
        cmd = New OleDbCommand("select * from Test1", cn)
        dr = cmd.ExecuteReader

        While dr.Read()
            ListBox1.Items.Add(dr(0) & vbTab & dr(1) & vbTab & dr(2))
        End While
        dr.Close()
        cn.Close()
    End Sub


    Private Sub btmSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btmSave.Click

        cn = New OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=Database11.mdb")
        cn.Open()
        str = "insert into Test1 (Name, Age) values ('" & txtName.Text & "','" & CInt(txtAge.Text) & "')"

        cmd = New OleDbCommand(str, cn)
        cmd.ExecuteNonQuery()
        MessageBox.Show("Saved")
        cn.Close()

    End Sub
End Class

--------------------------------------------------------

in the database, i have ID,Name and Age
(simple database)

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.