Deleting a record


                Deleting a  record  on a table is very crucial so in terms on coding we must be safe . there  are two types of deleting a record one is deleting all of the content of a specific table or we want to delete a specific record on a specific table.
                Now lets proceed for the first example we want to delete all of the content on the table so create a button with a caption of delete all, and another button to with a caption of Delete
Your form should look like this

Now first we will concentrate the most easiest part which is deleting all of the records, double click the button delete all.
If MsgBox("Are you sure you want to delete", vbYesNo, "delete") = vbYes Then
                ‘ first before deleting the content the database we want to ask the user if he/she is sure on deleting the record then if he press the button yes then that would be the time to execute this codes.

    Dim rstDelall As New ADODB.Recordset
                ‘declareation a of recordset
rstDelall.Open "Delete * from tbl_Products", con, 3, 3
                ‘ we are saying that we want to delete all the records on the specified table
   
Me.lvproducts.ListItems.Clear
                ‘ we need to clear the listview since we already deleted all the products
End If


Deleting specific Record
Dim product As String
‘declaration of product with a datatype of  string
product = Me.lvproducts.SelectedItem.SubItems(1)
‘product will now hold the value of the name of the selected  product on the listview
If MsgBox("are you sure you want to delete " & product, vbYesNo, "delete") = vbYes Then
    ‘ask the user if you really want to delete the selected product
Dim rstdel As New ADODB.Recordset
‘declaration of a recordser
    rstdel.Open "Select * from tbl_Products where fld_PID= " & Me.lvproducts.SelectedItem.Text & "", con, 3, 3
‘select statement that we want to get a specific record
    rstdel.Delete
‘delete is we are refering that we want to delete a single record
    Me.lvproducts.ListItems.Remove (Me.lvproducts.SelectedItem.Index)
‘remove the selected item on a listview.
End If


Were done..
Happy coding …

Walang komento:

Mag-post ng isang Komento