 |
BorlandTalk.com Borland discussion newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
aaa.aaaa Guest
|
Posted: Wed Jan 18, 2006 3:36 pm Post subject: TCustomADODataSet.Clone and Calculated fields |
|
|
Hy !!!
I would like to know why the clone method of TCustomADODataSet doesn't
clone the Calculated fields
example :
.......
procedure TForm1.Button2Click(Sender: TObject);
var
msg: string;
begin
if m_ADOQuery <> nil then
FreeAndNil(m_ADOQuery);
m_ADOQuery:= TADOQuery.Create(Nil);
Self.ADOQuery1.Open;
m_ADOQuery.Clone(Self.ADOQuery1);
DataSource1.DataSet:= m_ADOQuery;
msg:= 'ADOQuery1 Field Count' + IntToStr(ADOQuery1.Fields.Count);
msg:= msg + #13#10 + 'm_ADOQuery Field Count' +
IntToStr(m_ADOQuery.Fields.Count);
msg:= msg + #13#10 + 'ADOQuery1 FieldDefList Count' +
IntToStr(ADOQuery1.FieldDefList.Count);
msg:= msg + #13#10 + 'm_ADOQuery FieldDefList Count' +
IntToStr(m_ADOQuery.FieldDefList.Count);
MessageDlg(msg, mtInformation, [mbOK], 0); DataSource1.DataSet:= nil;
end;
The Calculated fields are not cloned how can i clone them ?
Thanks
Here bellow a part of the .dfm file
object ADOQuery1: TADOQuery
Connection = ADOConnection1
CursorType = ctStatic
OnCalcFields = ADOQuery1CalcFields
Parameters = <>
SQL.Strings = (
'SELECT EmployeeID, LastName , FirstName ,Title '
'FROM Employees')
Left = 64
Top = 88
object ADOQuery1EmployeeID: TAutoIncField
FieldName = 'EmployeeID'
ReadOnly = True
end
object ADOQuery1LastName: TWideStringField
FieldName = 'LastName'
end
object ADOQuery1FirstName: TWideStringField
FieldName = 'FirstName'
Size = 10
end
object ADOQuery1Title: TWideStringField
FieldName = 'Title'
Size = 30
end
object ADOQuery1Calc1: TStringField
FieldKind = fkCalculated
FieldName = 'Calc1'
Calculated = True
end
object ADOQuery1Calc2: TFloatField
FieldKind = fkCalculated
FieldName = 'Calc2'
Calculated = True
end
end
|
|
| Back to top |
|
 |
Steve Zimmelman Guest
|
Posted: Wed Jan 18, 2006 7:53 pm Post subject: Re: TCustomADODataSet.Clone and Calculated fields |
|
|
If you follow the code, you'll see that clone only replaces the RecordSet
property. It does not duplicate the Ado component.
From the Help:
"Call Clone to make the recordset of the calling ADO dataset component a
duplicate of the recordset active in another TCustomADODataSet descendant
component."
"aaa.aaaa" <aaa.aaa (AT) aaa (DOT) net> wrote
| Quote: | Hy !!!
I would like to know why the clone method of TCustomADODataSet doesn't
clone the Calculated fields
|
|
|
| Back to top |
|
 |
aaa.aaaa Guest
|
Posted: Thu Jan 19, 2006 8:49 am Post subject: Re: TCustomADODataSet.Clone and Calculated fields |
|
|
ok but if i try that
myClonedDataSet.FieldByName('MyOldCalutedField').AsValue
the systeme raise error "Field undetermined"
The Calculated fields are not cloned how can i clone them ?
Thanks
Steve Zimmelman wrote:
| Quote: | If you follow the code, you'll see that clone only replaces the RecordSet
property. It does not duplicate the Ado component.
From the Help:
"Call Clone to make the recordset of the calling ADO dataset component a
duplicate of the recordset active in another TCustomADODataSet descendant
component."
"aaa.aaaa" <aaa.aaa (AT) aaa (DOT) net> wrote in message
news:43ce5fef$1 (AT) newsgroups (DOT) borland.com...
Hy !!!
I would like to know why the clone method of TCustomADODataSet doesn't
clone the Calculated fields
|
|
|
| Back to top |
|
 |
Steve Zimmelman Guest
|
Posted: Thu Jan 19, 2006 7:31 pm Post subject: Re: TCustomADODataSet.Clone and Calculated fields |
|
|
You can only clone the actual recordset that is derived from the database.
Calculated fields belong to the component, not the recordset.
You can't clone calculated fields, you must create them just as you did in
the original Ado component and assign them to an OnCalcFields event method.
You might be able to use something like this:
CloneFields(SrcAdoQuery,DestAdoQuery);
DestAdoQuery.Clone(SrcAdoQuery);
(*** Not Tested ***)
Uses DB ;
Procedure CloneFields(DataSet,DestDS:TDataSet);
Var
iMax : Integer ;
AFld : TField ;
Begin
if (DataSet = nil) or (DataSet.FieldCount = 0) or not DataSet.Active then
exit;
If DataSet.FieldCount > 0 then begin
iMax := DataSet.FieldCount ;
End Else Begin
DataSet.FieldDefs.Update;
iMax := DataSet.FieldDefs.Count ;
End;
For i := 0 to (iMax - 1) Do Begin
AFld := DestDS.FindField(DataSet.Fields[i].FieldName) ;
If (AFld <> Nil) Then AFld.Free ;
AFld :=
DefaultFieldClasses[DataSet.Fields[i].DataType].Create(DestDS);
AFld.Name := DestDS.Name +DataSet.Fields[i].FieldName;
AFld.FieldName := DataSet.Fields[i].FieldName;
AFld.FieldKind := DataSet.Fields[i].FieldKind ;
If (AFld is TStringField) Then Begin
TStringField(AFld).Size := DataSet.Fields[i].Size ;
End Else If (AFld is TBlobField) Then Begin
TBlobField(AFld).Size := DataSet.Fields[i].Size ;
End;
AFld.DataSet := DestDS;
End;
End;
You will still need to create an OnCalcFields event in the dataset that
receives the cloned RecordSet.
-Steve-
"aaa.aaaa" <aaa.aaa (AT) aaa (DOT) net> wrote
| Quote: | ok but if i try that
myClonedDataSet.FieldByName('MyOldCalutedField').AsValue
the systeme raise error "Field undetermined"
The Calculated fields are not cloned how can i clone them ?
Thanks
|
|
|
| Back to top |
|
 |
aaa.aaaa Guest
|
Posted: Mon Jan 23, 2006 9:19 am Post subject: Re: TCustomADODataSet.Clone and Calculated fields |
|
|
Thanks a lot
I just modified one line
| Quote: | AFld := DestDS.FindField(DataSet.Fields[i].FieldName) ;
if (AFld = nil) then |
// because i think i don't have the right do free the field of the
//source Dataset
begin
| Quote: | AFld := >DefaultFieldClasses[DataSet.Fields[i].DataType].Create(DestDS);
AFld.Name := DestDS.Name +DataSet.Fields[i].FieldName;
AFld.FieldName := DataSet.Fields[i].FieldName;
AFld.FieldKind := DataSet.Fields[i].FieldKind ;
If (AFld is TStringField) Then Begin
TStringField(AFld).Size := DataSet.Fields[i].Size ;
End Else If (AFld is TBlobField) Then Begin
TBlobField(AFld).Size := DataSet.Fields[i].Size ;
End;
AFld.DataSet := DestDS;
end; |
Steve Zimmelman wrote:
| Quote: | You can only clone the actual recordset that is derived from the database.
Calculated fields belong to the component, not the recordset.
You can't clone calculated fields, you must create them just as you did in
the original Ado component and assign them to an OnCalcFields event method.
You might be able to use something like this:
CloneFields(SrcAdoQuery,DestAdoQuery);
DestAdoQuery.Clone(SrcAdoQuery);
(*** Not Tested ***)
Uses DB ;
Procedure CloneFields(DataSet,DestDS:TDataSet);
Var
iMax : Integer ;
AFld : TField ;
Begin
if (DataSet = nil) or (DataSet.FieldCount = 0) or not DataSet.Active then
exit;
If DataSet.FieldCount > 0 then begin
iMax := DataSet.FieldCount ;
End Else Begin
DataSet.FieldDefs.Update;
iMax := DataSet.FieldDefs.Count ;
End;
For i := 0 to (iMax - 1) Do Begin
AFld := DestDS.FindField(DataSet.Fields[i].FieldName) ;
If (AFld <> Nil) Then AFld.Free ;
AFld :=
DefaultFieldClasses[DataSet.Fields[i].DataType].Create(DestDS);
AFld.Name := DestDS.Name +DataSet.Fields[i].FieldName;
AFld.FieldName := DataSet.Fields[i].FieldName;
AFld.FieldKind := DataSet.Fields[i].FieldKind ;
If (AFld is TStringField) Then Begin
TStringField(AFld).Size := DataSet.Fields[i].Size ;
End Else If (AFld is TBlobField) Then Begin
TBlobField(AFld).Size := DataSet.Fields[i].Size ;
End;
AFld.DataSet := DestDS;
End;
End;
You will still need to create an OnCalcFields event in the dataset that
receives the cloned RecordSet.
-Steve-
"aaa.aaaa" <aaa.aaa (AT) aaa (DOT) net> wrote in message
news:43cf5200$1 (AT) newsgroups (DOT) borland.com...
ok but if i try that
myClonedDataSet.FieldByName('MyOldCalutedField').AsValue
the systeme raise error "Field undetermined"
The Calculated fields are not cloned how can i clone them ?
Thanks
|
|
|
| Back to top |
|
 |
Steve Zimmelman Guest
|
Posted: Tue Jan 24, 2006 2:53 pm Post subject: Re: TCustomADODataSet.Clone and Calculated fields |
|
|
As long as the DataSet is closed you can free whatever fields you want.
"aaa.aaaa" <aaa.aaa (AT) aaa (DOT) net> wrote
| Quote: | Thanks a lot
I just modified one line
AFld := DestDS.FindField(DataSet.Fields[i].FieldName) ;
if (AFld = nil) then
// because i think i don't have the right do free the field of the
//source Dataset
begin
AFld := >DefaultFieldClasses[DataSet.Fields[i].DataType].Create(DestDS);
AFld.Name := DestDS.Name +DataSet.Fields[i].FieldName;
AFld.FieldName := DataSet.Fields[i].FieldName;
AFld.FieldKind := DataSet.Fields[i].FieldKind ;
If (AFld is TStringField) Then Begin
TStringField(AFld).Size := DataSet.Fields[i].Size ;
End Else If (AFld is TBlobField) Then Begin
TBlobField(AFld).Size := DataSet.Fields[i].Size ;
End;
AFld.DataSet := DestDS;
end;
Steve Zimmelman wrote:
You can only clone the actual recordset that is derived from the
database. Calculated fields belong to the component, not the recordset.
You can't clone calculated fields, you must create them just as you did
in the original Ado component and assign them to an OnCalcFields event
method.
You might be able to use something like this:
CloneFields(SrcAdoQuery,DestAdoQuery);
DestAdoQuery.Clone(SrcAdoQuery);
(*** Not Tested ***)
Uses DB ;
Procedure CloneFields(DataSet,DestDS:TDataSet);
Var
iMax : Integer ;
AFld : TField ;
Begin
if (DataSet = nil) or (DataSet.FieldCount = 0) or not DataSet.Active
then exit;
If DataSet.FieldCount > 0 then begin
iMax := DataSet.FieldCount ;
End Else Begin
DataSet.FieldDefs.Update;
iMax := DataSet.FieldDefs.Count ;
End;
For i := 0 to (iMax - 1) Do Begin
AFld := DestDS.FindField(DataSet.Fields[i].FieldName) ;
If (AFld <> Nil) Then AFld.Free ;
AFld :=
DefaultFieldClasses[DataSet.Fields[i].DataType].Create(DestDS);
AFld.Name := DestDS.Name +DataSet.Fields[i].FieldName;
AFld.FieldName := DataSet.Fields[i].FieldName;
AFld.FieldKind := DataSet.Fields[i].FieldKind ;
If (AFld is TStringField) Then Begin
TStringField(AFld).Size := DataSet.Fields[i].Size ;
End Else If (AFld is TBlobField) Then Begin
TBlobField(AFld).Size := DataSet.Fields[i].Size ;
End;
AFld.DataSet := DestDS;
End;
End;
You will still need to create an OnCalcFields event in the dataset that
receives the cloned RecordSet.
-Steve-
"aaa.aaaa" <aaa.aaa (AT) aaa (DOT) net> wrote in message
news:43cf5200$1 (AT) newsgroups (DOT) borland.com...
ok but if i try that
myClonedDataSet.FieldByName('MyOldCalutedField').AsValue
the systeme raise error "Field undetermined"
The Calculated fields are not cloned how can i clone them ?
Thanks
|
|
|
| Back to top |
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
|