In order to demonstrate how we can manage data in the database we will add a few controls to the existing form that will allow us to modify some of the fields in the Contacts table. Note that I have purposely only added controls to modify some of the fields in the table. The reason for this is twofold.
First, it will be a good exercise for you to add the remaining fields to the interface once you are happy with your understanding of working with databases, and secondly, the code samples will be quite long winded if all fields were listed. After all, the aim of this tutorial is to show you some of the techniques that you can employ when working with databases and I am sure that your next database application will be somewhat different to a simple Address Book.
So, without further ado, start by adding three new buttons to the Address Book form as follows:
We will also modify our existing code slightly (not that there is much of it at the moment). Currently, we are retrieving all Contacts data within the Form Load event which is fine for displaying the initial data to the user. However, when we add new records or amend existing records, it will be quite awkward having to keep writing the same retrieval code again in order to show the changes. So the easiest approach would be to take the code that retrieves the contacts data and place it into a sub routine which we can then call from both the Form Load event and any other event that we see fit.
I have created the following routine called RetrieveContacts and moved the code from the Form Load event to this new routine:
and then added a call to the above routine within the Form Load event:
The last thing we need to do before moving on to modifying our data is to create a form that we can use to add new data and edit existing data. So, if you add a new form to your project and call it "ManageData", then add the following controls to the form:
it should then look similar to the following:
As you can see, we will only be concentrating on adding and updating three of the fields in the Contacts table. It will be a further exercise should you wish to complete the project to add the additional controls and modify the code accordingly to manage all of the fields in the Contacts table.