Data Control

Adding and deleting records

Up to this point, you have not written a single line of code. However, in order to add some extra functionality, you will have to write a grand total of two lines (well, 6 including the procedure names). First, add two command buttons to the form:

Name Caption
cmdAdd &Add
cmdDelete &Delete

In case your wondering, the ampersand (&) specifies the letter to be underlined in the command button, and therefore allowing the user to press Alt+A for Add, and Alt+D for delete if they wish. Then, double click the Add button to bring up the code window. You can now enter the following code:

Private Sub cmdAdd_Click()
     datTitles.Recordset.AddNew ' add a new record to the table
End Sub

Private Sub cmdDelete_Click()
     datTitles.Recordset.Delete ' delete current record
End Sub

After trying this, you will notice that when you delete an item you get some blank fields. This is because there is no current record. In order to avoid this, add

datTitles.Recordset.MoveNext

to the end of the cmdAdd_Click() Procedure

You might also like...

Comments

About the author

James Crowley

James Crowley United Kingdom

James first started this website when learning Visual Basic back in 1999 whilst studying his GCSEs. The site grew steadily over the years while being run as a hobby - to a regular monthly audien...

Interested in writing for us? Find out more.

Contribute

Why not write for us? Or you could submit an event or a user group in your area. Alternatively just tell us what you think!

Our tools

We've got automatic conversion tools to convert C# to VB.NET, VB.NET to C#. Also you can compress javascript and compress css and generate sql connection strings.

“Never trust a programmer in a suit.” - Anonymous