Deleting a Record
|
Deleting a record consists of removing it from a table
(or a form). To delete a record, you
combine the DELETE operator in the following primary formula:
DELETE FROM TableName
When this statement is executed, all records from the TableName
table would be removed. Here is an example:
Private Sub btnRemoveRecords_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles btnRemoveRecords.Click
Dim conVideos As New ADODB.ConnectionClass
conVideos.Open("Provider=Microsoft.Jet.OLEDB.4.0;" & _
"Data Source='C:\Programs\VideoCollection.mdb'", _
Nothing, Nothing, 0)
conVideos.Execute("DELETE FROM Videos;")
MsgBox("All records from the Videos table have been deleted.")
conVideos.Close()
End Sub
In this case, all records from a table named Videos in the current database would be deleted. An alternative to the
above formula is:
DELETE * FROM TableName
In this formula, you use the * operator as the column
placeholder. You can replace it with one or more names of columns but it
doesn't matter because the DELETE operator signifies that the whole
record will be deleted, regardless of the column name.
The TableName must be a valid name of a table
in the specified or the current database. Here is an example:
Private Sub btnRemoveRecords_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles btnRemoveRecords.Click
Dim conVideos As New ADODB.ConnectionClass
conVideos.Open("Provider=Microsoft.Jet.OLEDB.4.0;" & _
"Data Source='C:\Programs\VideoCollection.mdb'", _
Nothing, Nothing, 0)
conVideos.Execute("DELETE * FROM Videos3;")
MsgBox("All records from the Videos table have been deleted.")
conVideos.Close()
End Sub
If you execute this type of statement, all records
from the table would be deleted. You can
specify what record to remove from a table. To do this, use the following
formula of the DELETE operator:
DELETE * FROM TableName WHERE Condition
This time, the Condition factor allows you to set the
condition that would be applied to locate the record. Here is an example of specifying a condition to delete
a record:
Private Sub btnRemoveRecords_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles btnRemoveRecords.Click
Dim conVideos As New ADODB.ConnectionClass
conVideos.Open("Provider=Microsoft.Jet.OLEDB.4.0;" & _
"Data Source='C:\Programs\VideoCollection.mdb'", _
Nothing, Nothing, 0)
conVideos.Execute("DELETE * FROM Videos WHERE Director = 'Phillip Noyce';")
MsgBox("All videos directed by Phillip Noyce have been deleted.")
conVideos.Close()
End Sub
When this code runs, all videos directed by Phillip
Noyce would be deleted from the table. Instead of deleting all records like
this, you may want to remove only one particular video. To do this,
you must set a condition that sets that record apart. Once again, the
condition can be easily handled by the COUNTER column.
|
No comments:
Post a Comment