home *** CD-ROM | disk | FTP | other *** search
/ Delphi 5 for Professionals / DELPHI5.iso / Runimage / Delphi50 / Demos / Db / Cachedup / errform.pas < prev   
Encoding:
Pascal/Delphi Source File  |  1999-08-11  |  4.1 KB  |  141 lines

  1. unit ErrForm;
  2.  
  3. interface
  4.  
  5. uses
  6.   SysUtils, Windows, Messages, Classes, Graphics, Controls, Forms, Dialogs,
  7.   StdCtrls, Grids, DB, DBTables;
  8.  
  9. type
  10.   TUpdateErrorForm = class(TForm)
  11.     ErrorText: TLabel;
  12.     UpdateType: TLabel;
  13.     UpdateData: TStringGrid;
  14.     SkipButton: TButton;
  15.     RetryButton: TButton;
  16.     AbortButton: TButton;
  17.     procedure FormCreate(Sender: TObject);
  18.     procedure FormDestroy(Sender: TObject);
  19.     procedure UpdateDataSetEditText(Sender: TObject; ACol, ARow: Integer;
  20.       const Value: string);
  21.   private
  22.     FDataFields: TStringList;
  23.     procedure GetFieldValues(DataSet: TDataSet);
  24.     procedure SetFieldValues(DataSet: TDataSet);
  25.   public
  26.     function HandleError(DataSet: TDataSet; E: EDatabaseError;
  27.       UpdateKind: TUpdateKind): TUpdateAction;
  28.   end;
  29.  
  30. var
  31.   UpdateErrorForm: TUpdateErrorForm;
  32.  
  33. implementation
  34.  
  35. {$R *.DFM}
  36.  
  37. { Public and Private Methods }
  38.  
  39. { This method handles the error by displaying a dialog with information about
  40.   the error and then allowing the user to decide what course of action to take }
  41.  
  42. function TUpdateErrorForm.HandleError(DataSet: TDataSet; E: EDatabaseError;
  43.   UpdateKind: TUpdateKind): TUpdateAction;
  44. const
  45.   UpdateKindStr: array[TUpdateKind] of string = ('Modified', 'Inserted',
  46.     'Deleted');
  47. begin
  48.   { Put the error context information into the labels on the form }
  49.   UpdateType.Caption := UpdateKindStr[UpdateKind];
  50.   ErrorText.Caption := E.Message;
  51.   { Fill the string grid with the update field values }
  52.   GetFieldValues(DataSet);
  53.   ShowModal;
  54.   case ModalResult of
  55.     mrRetry:
  56.       begin
  57.         { If user wants to retry, then put any changed values from the
  58.           string grid back into the associated field's NewValue property }
  59.         SetFieldValues(DataSet);
  60.         Result := uaRetry;
  61.       end;
  62.     mrIgnore:
  63.       Result := uaSkip;
  64.     else
  65.       Result := uaAbort;
  66.   end;
  67. end;
  68.  
  69. { This fills in the string grid with data from the record being updated }
  70.  
  71. procedure TUpdateErrorForm.GetFieldValues(DataSet: TDataSet);
  72. var
  73.   I: Integer;
  74.   F: TField;
  75. begin
  76.   { Create a list of the data fields in the dataset, and store them in
  77.     a stringlist which we can use to determine which values the user
  78.     has edited }
  79.  
  80.   FDataFields.Clear;
  81.   for I := 0 to DataSet.FieldCount - 1 do
  82.     with Dataset.Fields[I] do
  83.       if (FieldKind = fkData) then
  84.         FDataFields.AddObject('', DataSet.Fields[I]);
  85.  
  86.   { Now fill up the string grid with the Old and New values of each field.
  87.     OldValue and NewValue are public properties of TDataSet which are used
  88.     from within the OnUpdateError event handler to determine what data a
  89.     user has updated.  We use the VarToStr RTL function to ensure that null
  90.     fields are displayed as blank strings }
  91.  
  92.   UpdateData.RowCount := FDataFields.Count + 1;
  93.   for I := 0 to FDataFields.Count - 1 do
  94.   begin
  95.     F := TField(FDataFields.Objects[I]);
  96.     UpdateData.Cells[0, I + 1] := VarToStr(F.NewValue);
  97.     UpdateData.Cells[1, I + 1] := VarToStr(F.OldValue);
  98.   end;
  99. end;
  100.  
  101. procedure TUpdateErrorForm.SetFieldValues(DataSet: TDataSet);
  102. var
  103.   I: Integer;
  104.   F: TField;
  105. begin
  106.   for I := 0 to FDataFields.Count - 1 do
  107.     { We set the string in the data field list to '*' for any fields the
  108.       user edited in the string grid.  Those are the only fields we need
  109.       to write back into the associated TField's NewValue property }
  110.     if FDataFields[I] = '*' then
  111.     begin
  112.       F := TField(FDataFields.Objects[I]);
  113.       F.NewValue := UpdateData.Cells[0, I + 1];
  114.     end;
  115. end;
  116.  
  117. { Event handlers }
  118.  
  119. procedure TUpdateErrorForm.FormCreate(Sender: TObject);
  120. begin
  121.   FDataFields := TStringList.Create;
  122.   { Fill in the titles of the string grid }
  123.   UpdateData.Cells[0,0] := 'New Value';
  124.   UpdateData.Cells[1,0] := 'Old Value';
  125. end;
  126.  
  127. procedure TUpdateErrorForm.FormDestroy(Sender: TObject);
  128. begin
  129.   FDataFields.Free;
  130. end;
  131.  
  132. procedure TUpdateErrorForm.UpdateDataSetEditText(Sender: TObject; ACol,
  133.   ARow: Integer; const Value: string);
  134. begin
  135.   { Set a flag in the list of datafields indicating that this value
  136.     was changed. }
  137.   FDataFields[ARow - 1] := '*';
  138. end;
  139.  
  140. end.
  141.