15.6.2.3 Delete a field part
Using the Field Editor fields editor The field parts that are created for the dataset part are displayed in the Fields list box in the field editor, and you can click the unwanted field parts if the user thinks some of them are unsuitable or no longer needed. Then click the right mouse button to eject the pop-up menu, select the Delete menu item from the pop-up menu, you can delete the corresponding field part, if you click the Select All menu item in the pop-up menu, and then select the Delete menu item, this will delete all the field parts that have been created. Once a field part is deleted, you can re-create it by clicking the Add Fields menu item, except that some of the properties previously set for the field part will no longer exist.
15.6.2.4 Define a new field part
The new Fields menu item in the pop-up menu in the Field Editor fields editor is used to create a fresh field widget for the dataset part ttable or Tquery, which we can use to create a new field part for the field that actually exists in the database table ( If you change the data type of a field so that its field values are displayed without the type conversion associated with them, we create a new field part using the Fields menu item primarily to create a calculated field. The calculated field does not correspond to a field that is actually present in the database table, and its field values are calculated based on the other field values in the table, and the specific calculation expression is determined by the user writing program code for the Oncalcfields event of the ttable part or tquery part.
The procedure for defining (creating) a calculated field is as follows:
1. Click the new Fields menu item in the Field editor, and Define the field dialog box as shown in Figure 15.11.
2. Enter the name of the new field part in the FieldName edit box, or select the name of an existing field part from the Drop-down list box.
3. Select a field type for the new field part in the FieldType list box.
4. Click the Calculated check box to confirm that the new field part you defined is a calculated field.
5. Click the OK button to create the calculated field part that is defined above, at which point the name of the field part is automatically added to the Fields list box in the field edit.
Create a new calculated field
After the new calculated field is created, it does not have any field values, we have to write the appropriate program code, based on the actual field in the Database table field values for the calculated field value of the field values, We write code for the Oncalcfields event in the dataset part of the calculated field to assign a value to the calculated field, as follows:
1. Select the dataset part ttable or Tquery
2. Click the event page for the dataset part
3. Double-click the Oncalcfields event to write an event-handling procedure for the ttable or tquery part
15.7 Treport parts and their applications
In general database applications include the ability to provide output reports for end users, when using Delphi to develop database applications, you can use a part called Treport to perform report functions. The specific format and content of the report is created by a dedicated report generation tool provided by Delphi Reprotsmith, which generates a report file for the specific format and content of the report, and then sets the corresponding property parameters for the Treport part to perform the reporting function by the treport part.
We can double-click the Treport part in the design phase, call the Reportsimith tool, or double-click the Reportsmith icon in the Delphi program group to invoke the Reportsmith tool to create a report file. Please refer to the instructions for the use of the REPORTSIMTH tool for the specific procedure and design method.
When we use the Treport part to perform report functions, we want to set some of the properties of the Treport part, these properties are:
ReportName property: Describes the name of the report file, which is the report file created with Reportsmith.
Reportdir property: Describes the name of the path where the report file resides.
Preview property: This is a Boolean property. If its value is true, the report is displayed only on the screen when the report function is performed, and if its value is false, the report content is printed on the default printer.
Autounload Property: A Boolean property that, when the value is true, automatically unloads the Reportsmith tool from memory after performing a report function, and when its value is false, the Reportsmith tool is not unloaded from memory after running a report function. In general, if your application has only one report or fewer reports to output, set the Autounload property to True, and set the Autounload property to False if the application wants to output more than one report at a time.
Initialvalues property: This is a string-type property that describes the variables used in the report file, each one describing a variable. Such as:
Reportvar: = Value;
To learn more about the process of creating and using report variables, see the section Create a report.
Treport part you need to invoke the Run method to actually perform a report function to output a report. As shown below:
Report1.run;
The important methods of treport parts are shown in table 15.10.
Table 15.10 method of Treport parts
━━━━━━━━━━━━━━━━━━━━━━━━━━━━
Method Features
────────────────────────────
Run Execute report function, output report
RunMacro send a macro command to the Reportimith tool
Connect pre-connected report file and database, not in output report
Need to log on to the database
SetVariable Change the description of the report variable
Recalcreport when the report variable is changed, the report is printed again
━━━━━━━━━━━━━━━━━━━━━━━━━━━━
There are other data access parts such as Tbatchmove parts, which are mainly used to move or copy sail data records between two database tables, see the use of local SQL Server for specific use.
15.8 Application Examples: multiple forms display the same database table
In applications, we often need to display the contents of the same database table in a different view, for example, to display the different fields of one record in a database table in both forms, we have to find a way to synchronize the data browsing parts in the two forms to display the values of the different fields of the same record in the database table synchronously. The following two rules are important if you want to display records in the same database table in a different view:
Multiple Tdatasource parts can access the same dataset part at the same time
When displaying the same table in more than one form, you must set up a tdatasource part for each form, and you need to set a ttable part for one of the forms.
For example, if you want to display a record of a database table in both forms Form1 and Form2, the easiest way to do this is to set up a tdatasource part for Form1 and Form2, called DataSource1, DataSource2, and set up a ttable part Table1 in Form1, connect Datasource1 and Table1 in Form1, set the Form2 dataset property of DataSource2 in Form1 in the process of program running, The code is as follows:
Format2.DataSource1.Dataset: = Form1.table1;
This way, when Table1 is opened, the same record in the database table can be displayed synchronously in two forms.
An example named TWOFORMS.DPR is in C:\Delphi\DEMos\DB\TwoForms (if Delphi is installed in another disk drive, you can find the example from the appropriate disk drive), It demonstrates a record of displaying the same database table in two forms. The application opens the Contry.db table in the first form, displays the name, Captial, and continent fields in the form, displays the area and population fields in the second form, and a button in the first form to open the second form. There are tdbnavigator parts in two forms for recording navigation.