Putting theBatch Move Component to Work

The BatchMove method and component are enormously useful addito your programming repertoire. For all of the additional functionality that the component offers, it is relatively simple to implement. The Copy Machine project that is built in the following steps wi experiment with the functions in two segments. The first will simply demonstrate the component and its copy mode. After this has been done, we will work with some of the error handling abilities of BatchMove.

Chapter 5—Dotal Access with Delphi M S 491

Start a new project and place two Data Source,! Table, and Grid components on the form similar to the form shown in Figure 5.17. Add a BatchMovd component, a BitBtnJ and a Button to round out the form.

Set the BitBtn component's Kind property to bkClosei

Point DBGridll to the Data Source II control and point the DataSourcel DataSeti property to Tablet, completing the troika. For the Table component, set the DatabaseName property to DBDEMOS and the Table Name property to COUNTRY.DB]

Point DBGrid2 to DataSource and set the DataSource'si DataSeti property to Table2j The Table2 component's DatabaseNaitHl property will also be set to DBDEMOS. Since we are going to createl a new table by the batch operation, the name that we will provide for the TabieName property will not be selected from the list. Add the name COPIES.DB to the property. Remember that this table will be a persistent structure, remaining on disk after the project is closed.

Select! the BatchMovell component. Set the Source property to Tablel. This will be the table from which the rows are going to be copied. The destination for records involved in the batch! operation will be defined in the Destination property. In this example, we'll set this value to TabIe2J Set the Mode property to batCopy]

The BatchMovd component is not activated. Instead, it is executed using the Execute method of the component. Since the user of the application usually initiates this process, we will duplicate this through the button's OnClickl event handler. Enter the following code for the handler to perform the two-part process. First, it executes the

Chapter 5—Dotal Access with Delphi M S 491

BatchMove process and then opens the new table to display it in the grid.

procedure TForml .ButtonClick (Sender: TObject); begin

BatchMovel.Execute;

7. Compile and execute the program. Click once on the Copy button to copy all of the rows from COUNTRY.DB to COPIES.DB.

The BatchMove component has error handling capabilities built in that the BatchMove method does not, making it much more attractive for use. In the next set of steps we are going to modify the copier project to raise an exception and let the application deal with it. Using the COPIES.DB table just built, we are going to force key violations to occur by appending the same set of records to the table. Since the Name column is the primary key for the COUNTRY table, each time a similar value is tried, a key violation error will occur. A table is de to hold them, KEYVIOLS.DB, and this table is displayed for the user to review. In a production application, you could allow the user to modi these records and you could repeat the batch process to cleanly hand le the exception situation.

Add a Label component to the right of the Copy button.

Select the BatchMove component. Change the value of the AbortOnKeyViol property to False. Enter the name KEYVIOLS.DB to the KeyViolTableName property. Finally, change the Mode property to batAppend.

Modify the Copy button's OnClick event handler as follows:

procedure TForml .ButtonlClick(Sender : begin

  1. Addlndex('Nameldx1, 'Name'
  2. Execute;

i xPrimary]

if BatchMovel.KeyViolCount <> 0 then 1 begin

MessageD1g('There were Key violations mtError, [mbOk], 0); Label¡.Caption := Concat( InstToStr(BatchMove1 .KeyV ioICount ), the

'Key violations');

TableZ.Close;

  1. TableName := BatchMovel.KeyViolTableName; Table2.0pen;
  2. Color := clAqua; end; end;

Compile and execute the application. When the key violations occur, you will be notified by a message dialog. The key violations table will be displayed in the second grid, changing its background color to announce the change.

Was this article helpful?

0 0

Post a comment