Visual basic allows us to manage databases created with different database programs such as MS Access, Dbase, Paradox and etc. In this lesson, we are not dealing with how to create database files but we will see how we can access database files in the VB environment.
In the following example, we will create a simple database application which enable one to browse customers' names. To create this application, insert the data control into the new form. Place the data control somewhere at the bottom of the form. Name the data control as data_navigator. To be able to use the data control, we need to connect it to a database. We can create a database file using any database application but I suggest we use the database files that come with VB6. Let select NWIND.MDB as our database file.
To connect the data control to this database, double-click the DatabaseName property in the properties window and select NWIND.MDB. Next, double-click on the RecordSource property to select the customers table from the database. You can also change the caption of the data control to anything , we use "Click to browse Customers" . After that, we place a label and change its caption to Customer Name. Finally, insert another label and name it as cus_name and leave the label empty as customers' names will appear here when we click the arrows on the data control.
Next, We need to bind this label to the data control for the application to work. To do this, open the label's DataSource and select data_navigator that will appear automatically. One more thing that we need to do is to bind the label to the correct field so that data in this field will appear on this label. To do this, open the DataField property and select ContactName. Now, press F5 and run the program. You should be able to browse all the customers' names by clicking the arrows on the data control.
|The Design Interface.
The Runtime Interface
You can also add other fields using exactly the same method. For example, you can add adress, city and telephone number to the database browser.
Copyright ® 2008 Dr.Liew Voon Kiong . All rights reserved |Contact: email@example.com