RecordSets are the foundation of database programming. Let’s look at an example that will help you visualize RecordSets and explore the Data control. The Data application, shown in Figure 17.3, is nothing less than a front end for an existing table in a database.
VB6 at Work: The Data1 Project
To build this application, follow these steps:
1. Start a new Standard EXE project and design a Form like the one shown in Figure 17.3. Start by drawing a Data control at the bottom of the Form. The Data control is your gateway to the database.
2. With the Data control selected, open the Properties window, locate its DatabaseName property, and then click on the ellipsis button to open the Open dialog box.
3. Select the NWlND database that comes with VisualBasic.
4. Locate theRecordsource property in the Properties window, and drop down the list of available entries. You’ll see a list’of all the tables in the NWIND database. Select the Customers table.
5. Place four textboxes on the Form, as shown in Figure 17.3.
6. Select the first text box, and in the Properties window locate its DataSource property. Set it to Data.
7. Set the DataField property of the textbox to CompanyName. The DataField property is a drop-down ~ with the names of all fields in the Customers table.
8. Set the DataSource property of the other three textboxes to Data, and set the DataField property to Country, Phone, and Fax.
Now run the application. The textboxes will display the values of the corresponding fields of the Customers table in the NWIND database. Using the Data control’s buttons, you can move through the records (rows) of the table. Clicking leftmost button displays the first record of the table, and clicking the button next to it displays the previous record. Clicking the rightmost button displays the last record of the table, and Clicking the button next to it displays the next record.
Not only can yO\!navigate through the tf\ble’s records with the Data control, you can also edit its fields. Change the value of a field, move to the next record, and then come back to the previous record. The changes you made to the record are saved to the database, and now you see the updated value of the record.
You should also attempt to enter invalid data in the table’s fields. The program won’t allow you to enter invalid values and will let you know why. If you attempt to enter a very long phone number, for the application displays a message indicating that the entry is too long for the field and gives’ you a chance to correct the value. The field was obviously declared with a limited length in the definition of the database. It is actually the database management system that issues the warning, not Visual Basic, and certainly not the application. You have yet to enter a single line of code.
Figure 17.4 shows the relationship among the TextBox controls on the Form, the Data control, and the database. The controls on the Form can’t see the database directly. Instead, they see the database through the Data control. The Data control, in turn, sees a RecordSet, which happens to be a table of the database. You’ll learn later how to specify other types of RecordSets for the Data control.
The Data Control’s Properties
The most important properties of the Data control are:
• DatabaseName, which specifies the database to be used
• RecordSource, which speci.fies the part of the database seen by the control
At any given the Data control is positioned at a single row (record) of its RecordSet. The user can move to any other record with the help of the navigation buttons on the Data control.
The textboxes see a field in the current row. Each time the Data control is repositioned in its RecordSet, the textboxes are updated. H the data in a textbox change, the new value is written to the database when the Data control is repositioned. The TextBox controls are connected to a field of the RecordSet through the Data control, and they are called data-bound (they have been bound to a field in the RecordSet).
The most important properties of a data-bound control are:
• DataSource, which is the name of a Data control through which the controls (e.g., TextBox, CheckBox) are bound to a Data control; in other words, it’s the name of a Data control through which the data-bound control “sees” the database.
• DataField, which is the name of a field in the RecordSet that the control displays and updates.
When TextBox controls are bound to a specific field, and they are called, data bound. There are other controls that you can bind to the Data control’s. If you want to prevent users of the application from editing the table, you can use Label controls. If the table contains Boolean fields (that have the values TruelFalse only), you can bind them to CheckBox controls. Some common controls aren’t data-bound. The ListBox control, for example, isn’t data-bound. Microsoft provides an enhanced version of the UstBox control, the data-bound Ust- Boxcontrol, which can be automatically populated with the values of a column in the RecordSet. You’ll see later how to use this control in your applications.
You can set the following properties of the Data control from the Properties window, or you can use them from within your code to manipulate the control.
EOF The EOF (End of File) property returns a True/False value that indicates whether the current record position is after the last record in a RecordSet object.
BOF The BOF (Beginning of File) property returns a True/False value that indicates whether the current record position is before the first record in a RecordSet object.
BOFAdion This property sets or returns a value indicating what action the Data control should take when the BOF property is True, which can be one of the actions in Table 17.1.