Subject Re: [IBO] how to update field values based on other fields
Author Helen Borrie
At 02:11 PM 30/01/2003 +0800, you wrote:
>Hi,
>
>I'm an IBO newbie and I'm used to having fields in the Fields Editor of the
>VCL's table and query components, but now I can't seem to find what I'm
>looking for in IBO's query component.
>
>Say I have 2 fields, Field1 and Field2, and I need to compute the value of
>Field2 based on a formula that uses the value of Field1. I'm looking for
>something like the VCL's OnChange event for fields in the Fields Editor. I
>want Field2 to display the computed value every time Field1 is modified. How
>can I achieve this effect in the native IB query? I've worked up something
>using the OnDataChange event of my query's TIB_DataSource, but is there a
>better way?
That is one way: that event occurs at the time the datasource updates the
dataset.

Native IBO doesn't use persistent field objects, as you realise, so there
aren't really events that one could say correspond to those TField event
properties.

Do you have the field declared to the query's CalculatedFields array? If
so, then you would put the calculation into the OnCalculateField event
handler of the query. Here's a simple example from the Contact sample:

procedure TdmContact.qrContactCalculateField(Sender: TIB_Statement;
ARow: TIB_Row; AField: TIB_Column);
begin
// Display the calculated field as the ID value for an example.
with AField do
if FieldName = 'STYLE' then
Assign( ARow.ByName( 'ID' ))
else
if FieldName = 'FULLNAME' then
AsString := ARow.ByName( 'FIRSTNAME' ).AsString + ' ' +
ARow.ByName( 'LASTNAME' ).AsString;
end;

Notice that this procedure refers to the Row property of the dataset. This
is advisable for calculated fields, since it ensures that all of the values
taking part in the calculation are actually those currently held in *that*
row. Using other variable containers can sometimes lead to the wrong
values being used.

The Boolean TIB_Column property IsModified can be useful in this context
too, since it will return true only if the value really did change; and
the OldValues array of the IB_Row can also come in handy.

You might like to consider calling CalculateFields in the AfterModify
events of the participating IB_Columns.

Helen