Delete Record Command in Microsoft Accces and Visual Basic

In the last post we have learned how to modify records in a Microsoft Access database, in this post we will learn how to delete records in a Microsoft Access database.

We will use the Delete command statement is also part of the Sql Statement.

Delete Statement Syntax

While the use of the Delete statement syntax is as follows:
DELETE [table.*]
FROM table
WHERE criteria


The parts of the Delete Statement:

     table
     Table name (optional) where the record will be removed
     table
     Name of the table where the record will be removed
     criteria
     The expression that determines which records will be deleted.

Same as the previous example of the table "TestTable", which fieldnya consists of:
The data are as follows:
From the example above table we will try to erase the data for id "E.2001.00004".

The syntax is as follows:

"Delete * From TestTable Where Id = 'E.2001.00004'"

Note the quotation value E.2001.00004 flanked by quote one, because the value of type string in a field in the table.

Example Source Insert Delete Statement In Program

Taking the form of an example of how to change the record source using the Update statement earlier, which has the following design:
Then double click the Delete button, and type the following command:

Private Sub button2_Click (ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CmdDelete.Click
Vhapus Dim As String = "Delete * From TestTable Where Id = '" & TxtEmployeeId.Text & "'"


Using Conn As New OleDbConnection(ConnStr)
Conn.Open()
Using Cmd As New OleDbCommand(vhapus, Conn)
Cmd.ExecuteNonQuery()
Cmd.CommandType = CommandType.Text
MsgBox("Record Deleted !", MsgBoxStyle.Information, "Delete Record")
TxtEmployeeId.Text = "" : TxtEmployeeName.Text = "" : TxtEmployeeAddress.Text = ""
TxtEmployeeAge.Text = "" : Cmb_Certificate.Text = ""
End Using
End Using

End Sub

When finished, try to run your project by pressing the F5 key, then input the ID with the value you want to delete to the table, and then click the Delete button, if there is no error message will be a message box that confirmed "Record Deleted" which indicates that the record has been removed from the table.

Previous
Next Post »