15th chapter-Application and programming of data access Parts (III.) (3)

Source: Internet
Author: User
Tags first row

15.6.1.4 Field Parts Access

The field part corresponds to the actual field in the database table, and the user wants to read and write the value of the field in the database table by accessing the corresponding field part. In the previous chapters we introduced two types of field widgets in Delphi's database application: One is a permanent field part created with the field editor, and the other is a field part that is dynamically generated as the dataset part is activated (opened). Access to a permanent field part can be invoked directly by using the name of the field part. Suppose we created a field part Table1company in the design phase that corresponds to the company field in the Customer.db table, the following code accesses the field value of the company field and displays the field value in the edit box part Edit1.

Edit1.text: = Table1company.value;

Because the company field is a string-type data that matches the data type in Edit1, you can read the field value directly using the Value property of the field part. If the type of the two variables does not match, use the transformation function in table 15.9 to read the field values. For example, to read the value of the Custno field in the Customer.db table and display it in the edit box Edit1, suppose we have created a Editor corresponding field part, Custno, with the field editor (Fields Table1custno), the program code is as follows:

Edit1.text: = table1custno.asstring;

It is relatively difficult to access dynamically generated field parts, because dynamically generated field parts do not have their own names, and we must use special means to get the corresponding field parts of each field in the database table and then access the fields. There are two types of methods commonly used:

Using the Fields property of the dataset part

Fieldbyname method for using a dataset part

1. Accessing the fields in a database table using the Fields property of a dataset part

The Fields property of a dataset part is the list of names of dynamic field parts corresponding to each field in the database table that is connected to the dataset part. So we can access each field part by fields the property's subscript (that is, the index number) to access each field in the database table, starting with the index number from 0, That is, the first field in the database table corresponds to the first row of the fields list, the 0 index, the second field corresponds to the fields index number 1, and so on. The following example accesses the first field in the Customer.db table and displays its field values in the edit box Edit1. Suppose Table1 is connected to the database table customer.db.

Edit1.text: = Table1.fields[0]. asstring;

The following code assigns the character value in the edit box Edit1 to the first field in the current record in the Customer.db table to implement modifying the field values in the Customer.db table.

Table1.fields[0]. Asstring: = Edit1.text;

2. Accessing a field part using the Fieldbyname method of a dataset part

In a method owned by a dataset part, there is a Fieldbyname method that is specifically used to access dynamically generated field parts in a dataset part, and when calling the Fieldbyname method, you must pass the field name in the database table as an argument to Fieldbyname, By calling the method, you can get the field part that corresponds to the field, so that by using the field part we can read and write the corresponding field value in the table, when we access the field part, we must know the name of each field in the database table, otherwise there is no way to call the method. Or based on the above assumptions. The following is the program code to access the Custno field in the Customer.db table:

Edit1.text: = Table1.fieldbyname (' Custno '). asstring;

Table1.fieldbyname (' Custno '). Asstring: = Edit1.text;

When you use both methods to access dynamically generated field parts, you can use the transformation function in table 15.9 to convert data types between variables and field values.

15.6. Use of the 2 field editor

The field editor (Fields Editor) is primarily used to create permanent field parts. As we know in the previous section, when a ttable or tquery part is connected to a database table, and the ttable or tquery part is activated (the active property is set to True or the Open method is invoked), Delphi dynamically creates the corresponding field parts for each field in the table, the field part contains a lot of information about the field, such as field value, field value display, edit format, and so on, sometimes we need to create a permanent field part in our application for more convenient and reliable access to each field in the database table. At this point we have to use the field editor to implement our vision. The main functions of the field editor are as follows:

Create a permanent field part

Modify the display properties of a persistent field, such as display formatting, display width, and so on

Delete a permanent field part

Add a new permanent field widget

Define calculated fields (not corresponding to the actual fields in the database table, and the field values are calculated based on the values of the other fields in the table)

15.6.2.1 Open the Field editor

There are two ways to open field edits for the ttable and tquery parts:

Double-click the Ttable or Tquery part with the left mouse button

Select the ttable part or Tquery part, then right-click and choose Fields from the pop-up menu Editor

Field Editor fields Editor is opened, the name of the form and the name of the dataset part appear on the title of the window.

The Fields list box in the Field editor fields Itor is used to display the name of a permanent field part that has been created. Field Editor Fields editor is opened for the first time, the list box is empty because the field parts that preceded it are dynamically generated, so long as there are field parts in the Fields list box, only the field values for the fields listed in fields are displayed in the data browsing part that is connected to the dataset part. In the Fields list box, you can change the order in which the field values are displayed in the data navigation part by dragging and dropping the name of the field part, such as displaying the values of each field in the Tdbgrid part based on the order of each field in the Fields list box.

The navigation buttons above the Field Editor fields editor form are used to move the record pointer in a ttable or tquery part by using the navigation buttons to move the record pointer forward, backward, or move to the first record or last record place.

pop-up menu in Field Editor

15.6.2.2 Add Field Parts

The Add Fields menu item in the Field Editor fields editor is used to add a field part to a dataset part, and when you click the Add Fields menu item, the Add Field Part dialog box opens, as shown in Figure 15.9. The Available fields list box displays all of the fields in the dataset part ttable or Tquery that are currently available to create permanent field parts, that is, the Available Fields list box displays fields that are actually present in the database table. and the corresponding permanent field parts have not been created for these fields. All fields in the default state are selected for creating the corresponding permanent field part, and clicking the field name in the mouse can optionally create its corresponding permanent field widget, after selecting the relevant field name. You can create a permanent field part by clicking the OK button.

Add Field Part dialog box for field editor

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.