Section 2 Browsing and Navigation
There are two different ways to create forms that access database data. The first method is to use Database Form Expert. Unfortunately, database form experts can only handle the BDE-aware (visual BDE) subset of the dataset component. The second method is to manually place and connect all data components.
Defining the User Interface
We'll build our own data browsing form in three steps. The first step is to define the user interface for the form; the second step is to add and configure data access components; the third and final step is to add data visualization controls.
Before you begin, close any open projects. Then proceed to the following steps:
DBNavigator1.DataSource = DataSource1
DBGrid1.DataSource = DataSource1
DataSource1.DataSet = ADOTable1
ADOTable1.Connection = ADOConnection1
ADOConnection1.ConnectionString = ...
ADOConnection1.LoginPRompt = False
ADOTable1.Table = 'Applications'
Note: As discussed in Chapter 2, the ConnectionString property indicates the physical location where the data is stored and how we access it. You can use the same connection string as in Chapter 2, or create one yourself by calling the connection string editor.
Set the LoginPrompt property of the ADOConnection component to False to prevent the database login interface from appearing. Since we haven't set any password for the database, we don't need a login prompt.
Fields
When we want to provide users with the entire record set (data in the table), we can use the DBGrid component. Even though we can use DBGrid to let our users add, edit and delete records in the table - the best way is to use Field objects for each field in the table. Field objects are mostly used to control data display and editing in applications. By using the Fields Editor, we can set a fixed list of field objects for each column in the table. The field editor can be called by double-clicking the DataSet component. To add fields to a dataset's fixed field list, right-click the list and select Add Fields.
In addition to displaying all the data in the table (in DBGrid) for the user, we also need to use field-oriented (field-oriented) data visualization components such as edit boxes. For example, the DBEdit component is the data visualization version of the TEdit class. It is the building block of any data entry program.
The simplest way to place a DBEdit in a form and connect it to a table field is as follows:
When you drop the Name field on the label sheet, Delphi will place a Label and a DBEdit component on it. The Caption of the Label component is the same as the DisplayLabel property of the dragged field. The DBEdit component is connected to the data source of the dataset through its DataSource property. If you select multiple fields from the Field Editor and place them on the form, Delphi will place as many Label/DBEdit components on the form.
"It's alive!"
OK, all we need to do now is activate the connection and scroll through the records. The Active property of a DataSet (ADOTable) component indicates whether we are connected to a table. Setting Active to True or calling the Open method will cause the ADOConnection component's CONonnected property to be True—and display the data in the associated data visualization control.
First, Move by, Last,...
Now we make the final preparations. Next we learn how to browse the record set.
The DBNavigator component provides a friendly and simple tool for browsing record sets. In addition to its browsing capabilities, DBNavigator also provides methods to manipulate data, such as inserting, deleting, or canceling changes. For example, if we click the Delete button, the corresponding record will be deleted from the record set. Each button is optional and you can mix and match them however you like.
With button settings we can jump to the last record or move to the previous record. For example, clicking the Last button sets the current record to the last record in the recordset and disables the Last and Next buttons. Clicking the Last button has the same effect as calling the Last method of the data set.
It should be noted that one of the browsing operations that DBNavigator cannot handle is moving records forward or backward by a certain number of intervals. The MoveBy method of a dataset is used to position any record relative to the current record in the recordset.
That's it for this chapter. Now we are ready to learn the editing and querying of the dataset, which will be explained in the next chapters of this tutorial...
December 26, 2002 20:49