Subject Re: [IBO] Calculated fields problem..
Author Helen Borrie
At 04:32 PM 20/10/2004 -0500, you wrote:

>I'm finally evaluating IBO along with embedded FB. So far, everything is
>going fine. I do have one slight "glitch" that I can't seem to work out
>involving calculated fields. Hopefully I can cover all the needed
>information the first time.
>
>Simple table structure:
>
>CREATE TABLE Employee
>(
> ID INT NOT NULL,
> EmpNum VARCHAR(10),
> FirstName VARCHAR(20),
> LastName VARCHAR(20),
> MI CHAR(1),
> Street1 VARCHAR(40),
> Street2 VARCHAR(40),
> City VARCHAR(40),
> STATE CHAR(2),
> ZIPCODE CHAR(12),
> HOMEPHONE CHAR(10),
> HireDate DATE,
> TerminationDate DATE,
> Comment BLOB SUB_TYPE TEXT
>)!!
>
>
>I created a form for entering/editing employees. Using one TIB_Query and one
>TIB_Datasource, I tied them to a TIB_Grid on one side of the form for
>selecting employees (read-only grid) and edit (TIB_Edit, etc) controls on
>the other side of the form.
>
>Here is the SQL for the TIB_Query:
>
>SELECT ID
> , EMPNUM
> , FIRSTNAME
> , LASTNAME
> , MI
> , STREET1
> , STREET2
> , CITY
> , STATE
> , ZIPCODE
> , HOMEPHONE
> , HIREDATE
> , TERMINATIONDATE
> , COMMENT
>FROM EMPLOYEE
>ORDER BY LASTNAME, FIRSTNAME, MI
>
>RequestLive is set to true and the KeyLinks property is set to ID and
>everything updates, deletes, inserts just fine. The only problem is the
>calculated field for displaying the full name. I have the CalculateAllFields
>set to true just to force calculating whenever any field changes. (Do I
>understand this property right?)
>
>The CalculatedFields property holds the following:
>
>FULLNAME VARCHAR(45)
>
>The OnCalculateField procedure is :
>
>procedure TfrmWorkers.WorkersCalculateField(Sender: TIB_Statement;
> ARow: TIB_Row; AField: TIB_Column);
>begin
> if Assigned(AField) then
> if (AField.FieldName = 'FULLNAME') then begin
> AField.AsString := Workers.FieldByName('FirstName').AsString + ' ' +
>Workers.FieldByName('MI').AsString + ' ' +
>Workers.FieldByName('LastName').AsString;
> WorkerGrid.Update; // not sure if this is needed...
> end;
>end;
>
>
>When I first open the form, the grid does not show the full names of the
>employees. If you click on the blank rows, the information shows up in the
>TIB_Edits to the right, but the full name won't show in the grid until you
>actually change a field, or tab into/out of one of the name fields. I have
>an OnExit handler for them that calls the TIB_Query.CalculateFields. If I
>close the query and reopen the query (for instance, to change the sort order
>of the grid), the full names in the grid disappear again.
>
>Surely this must be something simple that I am doing wrong or not setting
>some property or other.

Yes, it is a simple thing. CalculateFields operates on a row, so pick up
the ARow argument and operate on that. OnCalculateField gets called once
for each field in the CalcFields array, so AField gives you a reference to
the calculated field.
I hope you'll forgive me for pushing the guts of this out into a separate
function. I get anxiety symptoms when I find myself writing event handlers
with lots of lines in them. :-)

procedure TfrmWorkers.WorkersCalculateField(Sender: TIB_Statement;
ARow: TIB_Row; AField: TIB_Column);
begin
// test not appropriate if Assigned(AField) then
if AField.Name = 'FULLNAME' then
with ARow do
begin
ByName('FULLNAME') := ConcatNames(
ByName('FirstName').AsString,
ByName('MI').AsString,
ByName('LastName').AsString);
end;
end;

function MyDM.ConcatNames (
String1, String2, String3: String): String;
begin
Result := EmptyStr;
if length(String1) > 0 then
Result := String1;
if length (String2) > 0 then
begin
if length(Result) > 0 then
Result := Result + ' ' ;
Result := Result + String2;
end;
if length(String3) > 0 then
begin
if length(Result) > 0 then
Result := Result + ' ' ;
Result := Result + String3;
end;
end;

Also, call CalculateFields whenever you need the calculation done. IBO
should do it automatically on Open and Refresh (check, though). However,
because a calculated field isn't a dataset object, you can make it do its
stuff at other times as well.


>BTW, I have just downloaded IBObjects within the last 2 weeks so it should
>be the latest version, IB 4.3 Aa, I believe.
>
>Not to disappoint anyone else, but I'm rooting for Helen to figure this out
>since she's so smart with these types of problems. :)

Hmmm.....

Helen