14th Chapter-Simple database application creation and Mastapp Introduction (i) (2)

Source: Internet
Author: User
Tags connect

14.1.3 Run the program

Save the file, name the code unit as CUST.PAS, name the project name CUSTPRJ.DPR, and then press F9 to compile and run the program. After the program is executed, we can use the scroll bar or the keyboard to move the key between the fields and records. However, the data in the table cannot be modified because the Datasouc1.autoedit1 property has been set to false.

The three parts in the Cust program have their own special uses, and the related properties of the three components are internally interconnected to generate the final application. The ttable part connects the actual database tables on the disk and the channels of other parts in the application. The ttable part has methods for opening and closing, reading, updating, and other processing of disk database files.

The Tdatasource part is a bridge connecting ttable parts and data browsing parts such as Tdbgrid parts. The Tdbgrid part is used to display data information from a database table, which provides an intuitive interface to the application. Figure 14.2 illustrates the relationship between the three parts.

Internal relationships between three parts in a cust program

The wonderful thing about the TDBGrid part is that it knows how to get the next or previous record in the database table, and we can do that with the scroll bar or the arrow keys. Tdbgrid parts do not know how to add, delete, and modify records. If you want the Cust program to be able to modify records in a database table, you can achieve the goal by setting the Autoedit property of the Datasource1 part to True and recompiling and running the program. Use the arrow keys to position the DBGrid high brightness bar on a field, and then type the new value, which will be replaced with the new value you typed, and the information that will be automatically written to the database table when you move to another record. If you want to discard your changes, just click the Escape key before you leave the field.

If you want to add a new record to a table, you can move the highlight bar to a blank record at the bottom of the grid and enter the relevant field values for the new record. You can also insert a new record after a user-specified record, as long as the highlight bar is positioned on the specified record, and the INS key allows you to insert a new record after that record.

When you delete a record, position the high brightness bar on any field of the record you want to delete, press CTRL + DEL, and the protection information appears, and we can confirm that you really want to delete the record.

Tdbgrid provides users with a more complete capability to control whether or not to edit, add, or delete records. To prevent any modification to the database table, set the ReadOnly property of the Tdbgrid part to True and set Option.dgediting to False (this will provide us with a read-only database table browser instead of a database editor, but it implies an increase, Ability to edit and delete records). The various combinations of these properties and option attributes for the Tdbgrid part make it easy for us to browse, edit, and manipulate database tables efficiently.

If we often use interfaces like spreadsheets to display and edit data records, the TDBGrid widget is a handy tool, but it's not the friendliest user interface, and if you want a more graceful and intuitive interface, we can also use a separate data browsing widget to display the values of each field in the database table. and uses the Tdbnavigator part to control the access to the database table.

14.2 using the Tdbnavigator part to create an access program

We can improve the Cust program so that it only displays one customer's record information in a dialog box at a time, and uses a Tdbnavigator widget to control access to records-allowing us to select a record to display or edit and to add and delete records. The completed application form.

Enhanced Cust Program

14.2.1 Create an application form

We can create it very quickly because we are already familiar with how to create forms, we first put all the parts in the form and then set their properties.

Start a new project, set the form Form1 the Name property to Customerform2, Caption property to CustomerForm2. Then select a DataSource part and a table part from the data access page on the part palette to put it in the upper-right corner of the form. Then select the Dbnatvigator part from the Data controls page and place it in the upper-left corner of the form.

The remaining parts of the form are shown in Figure 14.3. They are tdbedit and tlabel parts, creating and arranging parts as shown in Figure 14.3, respectively naming dbedit parts for Editcustno, Editcompany, EDITADDR1, EDITADDR2, editcity, editstate , Editzip, Editcountry, Editphone, Editfax, Edittaxrate, Editcontact.

Now we're going to connect ttable parts and tdatasource parts and then connect all the data browsing and datasource parts. Sets the properties of the Tbnavigator and Tdbedit parts, and their datasource properties are set to DataSouce1. The last thing we're going to do is connect the Tdbedit parts in the form and the names of the fields they correspond to in the database table, and do so by setting the DataField property of the Tdbedit part. For example, to connect a custno field in a tdbedit part and a database table named Editcustno, the steps are as follows:

① Select the Editcustno part in the form.

② in the Object Inspector form, click the arrow to the right of the DataField property.

③ Select the Custno field name from the Drop-down list.

Perform these actions on the other Tdbedit parts in the form to connect to their corresponding fields, and then save the file. The name code unit is named Cust2.pas and the named Project name is CUSPRJ2.DPR.

14.2.2 use the Tdbnavigator part to move the record pointer

After the above program runs, the first record in the database table is displayed in the data browsing part. You can move between fields by using the TAB key, but you cannot edit fields. Because in order to prevent accidental modification, the Table1 Autoedit property value is set to False. If you want to edit, insert, and delete records in a database table, or if you want to display another record in a database table, you need to press the function buttons that correspond to these features on the tdbnvigator part. The buttons on the Tdbnavigator part and their functions are shown in Figure 14.4.

The buttons in the Tdbnavigator

Most features of the tdbnavigator part can be easily identified according to its button's icon, and the Tdbnavigator widget itself can perceive many things, such as whether the current pointer is at the beginning or the end of a database table. If the user is viewing the last record in the database table, the next and previous buttons will be dimmed and become inactive. Similarly, if the user is currently browsing the first record in the database table, the Tdbnavigator and previous buttons will be dimmed and inactive. For more detailed instructions on how each button works, check the online Help.

If the user wants to modify the current record, click the Edit button for the Tdbnavigator part, and then complete the changes that need to be made, and then, after making the changes, click the Post button to write the changes to the actual database table (update the records in the actual database table in the database terminology called "Posting" Record is POSTT). If you want to cancel your changes, click the Cancel button. The Cancel button only cancels the changes made to the record since the last posting to the database table. For example, if the user has modified the Custno field and clicked the Post button, and then modified the company field and pressed cancel, only changes to company will be canceled. That is, once the modification is written to the database table, and then press the Cancle button to cancel the changes to the record, in order to revert to the previous state, the user must edit the change record. It is important to note that when the user modifies the current record and moves to another record, the Tdbnavigaator part automatically changes the record by posting the user. For example, if we modify the company field for the record and do not press the Post button to update the record in the table, but move to the next record, the user's changes to the record are automatically written to the database table.

14.2.3 Custom Tdbnavigator Parts

The buttons in the Tdbnavigator part are convenient for our developers, but not necessarily for the end users of the program. To help end users or novice users more easily and efficiently use the Tdbnavigator widget, we can set the Showhint property of the Tdbnavigator part to True, This prompts the button on the screen when the mouse cursor hovers over about 1 seconds on a button on the tdbnavigator part. If we do not want to use the hint information embedded in the tdbnavigator part itself, we can also set the Hints property of the Tdbnavigtor part and specify a specific hint for each button to help the user to use the Tdbnavigator part.

There are multiple feature buttons in the Tdbnavigator part, but not all buttons are required for every database application, especially those that do not allow you to modify data in a table, or modify a database application that is only under very strict control. We can determine which button buttons to display in Tdbnavigator by setting the Visiblebuttons property of the Tdbnavigator part. For example, if we don't allow the user to modify the records in the table, we don't need the Add, Delete, Post, Cancel, or Refresh buttons, and we set the button's Visiblebuttons property to False. These buttons will not appear in the Tdbnavigator part.

The Tdbnavigator part's Confirmdelete property and the Delete button are useful for deleting records from a database table, when the Confirmdelete property is set to True (the default setting), When the user clicks the Delete button to try to delete the current record, Delphi pops up a confirmation box asking the user to confirm that they really want to delete the current record. This makes it much safer for the user to delete records. If the user does not want a confirmation box to appear when the delete button is pressed, it is OK to set the Confirmdelete to False.

There are also some attributes that can be used to customize the appearance and performance of the tdbnavigator part, see the online Help for more information.

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.