I am attempting to define a ClientDataSet component in a form at runtime. I can successfully define all the fields and operate the ClientDataSet in the VCL form program, however when I attempt to add an event handler for events such as AfterInsert to the code, the compiler objects to my format.
The clientDataset is created in this procedure:
procedure TForm1.CreateNestedDataSets;
begin
cdsTables := TClientDataSet.Create(Self);
cdsNotes := TClientDataSet.Create(cdsTables); //nested dataset
//Define Tables
with TFloatField.Create(Self) do
begin
Name := 'TblID';
FieldKind := fkData;
FieldName := 'ID';
DataSet := cdsTables;
Required := True;
end;
... //define other fields for cdsTables & nested clientdataset cdsNotes
cdsNotes.AfterInsert := cdsNotesAfterInsert(cdsNotes: TDataSet);
//Create the ClientDataSet and its nested datasets
cdsTables.CreateDataSet;
//This is problem code line:
cdsNotes.AfterInsert := cdsNotesAfterInsert;
//Configure the DataSources
dsTables.DataSet := cdsTables;
dsNotes.DataSet := cdsNotes;
end;
Various forum discussions have suggested approaches such as this example:
MyLabel := TLabel.Create(self);
MyLabel.OnClick := MyLabelClick;
In the case of AfterInsert there is an argument included. If I generate the event at design time, Delphi generates:
procedure TForm1.ClientDataSet1AfterInsert(DataSet: TDataSet);
Attempting to duplicate the suggestion above I tried this approach which generates a compiler error:
cdsNotes.AfterInsert := cdsNotesAfterInsert;
Incompatible types parameter list differs
Other formats also generate errors:
cdsNotes.AfterInsert := cdsNotesAfterInsert(DataSet: TDataSet);
Too many actual parameters
I have tried other variations withan assorment of error messages. This is my first attempt at defining an event and I am not certain I understand how to handle the declaration. I beleive the actual procedure I declare to implement the event 'cdsNotesAfterInsert' does not require any parameter since it is tied
to the clientdataset cdsNotes. Please correct me if I am wrong.
Here is a complete form unit with the offending code
unit ForumTest;
interface
uses
Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Data.DB, DBClient;
type
TForm1 = class(TForm)
procedure FormCreate(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
cdsTables : tclientDataset;
cdsNotes :tclientDataset;
procedure CreateNestedDataSets;
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
procedure TForm1.CreateNestedDataSets;
begin
cdsTables := TClientDataSet.Create(Self);
cdsNotes := TClientDataSet.Create(cdsTables);
//Define Tables
with TFloatField.Create(Self) do
begin
Name := 'TblID';
FieldKind := fkData;
FieldName := 'ID';
DataSet := cdsTables;
Required := True;
end;
with TFloatField.Create(Self) do
begin
Name := 'TblParentID';
FieldKind := fkData;
FieldName := 'Parent';
DataSet := cdsTables;
Required := false;
end;
with TStringField.Create(Self) do
begin
Name := 'TblTitle';
FieldKind := fkData;
FieldName := 'Title';
Size := 40;
DataSet := cdsTables;
Required := True;
end;
with TStringField.Create(Self) do
begin
Name := 'TblFilename';
FieldKind := fkData;
FieldName := 'Filename';
Size := 80;
DataSet := cdsTables;
Required := False;
end;
//Note: For TDataSetFields, FieldKind is fkDataSet by default
with TDataSetField.Create(Self) do
begin
Name := 'TblNotes';
FieldName := 'NestedDataSet';
DataSet := cdsTables;
end;
//Define Notes
cdsNotes.DataSetField := TDataSetField(FindComponent('TblNotes'));
with TFloatField.Create(Self) do
begin
Name := 'NoteID';
FieldKind := fkData;
FieldName := 'Note ID';
DataSet := cdsNotes;
Required := True;
end;
with TStringField.Create(Self) do
begin
Name := 'NoteTxt';
FieldKind := fkData;
FieldName := 'Notes';
DataSet := cdsNotes;
Size := 40;
end;
cdsNotes.AfterInsert := cdsNotesAfterInsert(cdsNotes: TDataSet);
//Create the ClientDataSet and its nested datasets
cdsTables.CreateDataSet;
//Configure the DataSources
dsTables.DataSet := cdsTables;
dsNotes.DataSet := cdsNotes;
end;
procedure TForm1.FormCreate(Sender: TObject);
begin
CreateNestedDataSets;
end;
end.
This is specifically to answer your point about how to assign some self-written code to a TClientDataSet's AfterInsert event property.
If you look up TClientDataSet.AfterInsert in the online help, you'll see that it is defined as a TDataSetNotifyEvent, which in turn is defined as
type TDataSetNotifyEvent = procedure(DataSet: TDataSet) of object
The significance of the of object
is that the procedure must be a method of an object (for which read "class"), not a stand-alone procedure/method as you seemed to be describing in a comment.
To be assignment-compatible with a TDataSetNotifyEvent, your procedure needs to have a matching code "signature ", that is, it must be a procedure of a class (not a function of a class) and have the exact same parameters, in this case a single TDataSet parameter.
So pulling that all together, all you need is something like
type
TForm1 = class(TForm)
ClientDataSet1: TClientDataSet;
procedure FormCreate(Sender: TObject);
protected
procedure MyInsertHandler(ADataSet : TDataSet);
end;
[...]
procedure TForm1.FormCreate(Sender: TObject);
begin
ClientDataSet1.AfterInsert := MyInsertHandler;
end;
procedure TForm1.MyInsertHandler(ADataSet: TDataSet);
begin
// Your code goes here, e.g.
Caption := ADataSet.Name + ' after insert';
end;
Simple as that.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With