Members

Technology Zones

IBM Learning Center

Articles

Hosted By

MaximumASP

Info

Rated
Read 61,035 times

Contents

Related Categories

Data Control - Adding and deleting records

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

James first started writing tutorials on Visual Basic in 1999 whilst starting this website (then known as VB Web). Since then, the site has grown rapidly, and James has written numerous tutorials, articles and reviews on VB, PHP, ASP and C#. In October 2003, James formed the company Developer Fusion Ltd, which owns this website, and also offers various development services. In his spare time, he's a 3rd year undergraduate studying Computer Science in the UK. He's also a Visual Basic MVP.

Comments

  • Re: [71] Data Control

    Posted by Bhaskerit on 28 Jun 2007

    Hi ! All,


    I'm new in VB Programming and trying with all your given examples. I'm trying to create a database project. I've created a table in Access 2000 and wants to attatch it with my VB ...

  • hi

    Posted by Regie on 28 Jul 2005

    im a newbie in vb, im working now with the examples above, my objective is to save,add, delete files to the database i can't do it. there is a propmted error invalid or unqualified references.

  • hi there

    Posted by Regie on 28 Jul 2005

    i am a newbie in vb,and now im currently working with the tutorials that are post above, my objective is that i want to save, add or delete a file to my stand-alone database. thanks for fast replies.

  • Posted by chandrababu on 28 Jul 2005

    At least give some explanation about the environment or
    what is you objective.

  • Error

    Posted by Regie on 28 Jul 2005

    hi there, why is that i can't add or save new file? the error propmt is
    "invalid or unqualified reference." plz send me an answer or email it to me plz..hoping to hear from you. ASAP..thanks