Modify database records
14.6.1 Edit Method POST method
To allow the user to modify the records in the database table through a program, the tTable part must be in the editing state. In most cases, database tables are opened in the browser (read-only mode) mode. That is to say, each of its fields can be read and cannot be edited or modified. You can call the Edit Method to edit the tTable part. After the tTable part is edited, you can modify the records pointed to by the current record pointer through the program, however, the modified records are not immediately written to the actual database table on the disk. To save the modification to the record, you must call the POST method. The post method writes the modification to the actual database table.
In general, the program used to scan the entire database table and modify a field for each record is as follows:
With table do
Begin
Disablecontrols; {invalidate other parts during record modification}
First; {point the record pointer to the first record}
While not EOF do
Begin
<Read a record field value to a variable>
<Make appropriate changes>
Edit; {set the tTable part to edit status}
<Write the modified field value back to its corresponding field>
Post; {write the modified record back to the database}
Next; {modify next record}
End;
Enablecontrols; {restore functions of other parts}
End;
The program operates on the tTable part. Therefore, it is meaningful to use the with statement to prevent the spread of errors. Pay attention to the use of the disablecontrols and enablecontrols methods. The disablecontrols method disconnects the tTable part from the tdatasource part when the program modifies the record in the tTable part. Otherwise, after each modification to the tTable, The tdatasource component will update the display content of all data browsing parts in the form, which will greatly slow down the processing process and waste time. The enablecontrols method is the opposite of the disablecontrole method. It is used to restore the link between the tTable part and the tdatasource component and update and display all the data browsing parts.
The first method is to move the record pointer to the first record in the database table, so that the program starts to modify the first record in the table. The next method is used to move the record pointer from the current record to the next record. This ensures that the first record in the table is modified one by one until the last record is modified. If you do not call the next method, the program will be stuck in endless loops.