Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Shaun E on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Normal Behavior?

Status
Not open for further replies.

lespaul

Programmer
Feb 4, 2002
7,083
US
I have a DBGrid that displays working hours for jurors (total hours is a calculated field in the query):


Code:
Date       Type      Time In      Time Out     Total Hours
1/18/2005  ORIENT    8:30 AM      4:00 PM        7.5
1/19/2005  JUROR    11:00 AM      5:30 PM        6.5
1/20/2005  JUROR    11:00 AM      2:30 PM        3.5
1/21/2005  DLYPNL    9:30 AM      1:00 PM        3.5
1/25/2005  DLYPNL    2:00 PM      4:00 PM        2.0
1/26/2005  JUROR     1:00 PM      3:00 PM        2.0
1/27/2005  DLYPNL    2:00 PM      4:00 PM        2.0

on the same form is a TOTAL HOURS display. I calculate this total in the calculated field code:

Code:
procedure TfrmJurorInformation.qryJurorHoursCalcFields(DataSet: TDataSet);
begin
  with qryJurorHours do
  begin
    if FieldByName('TIMEOUT').AsInteger <> 0 then
      FieldByName('TotalTime').Value := FloatToStr((HourSpan(IntToTime(FieldByName('TimeIn').AsInteger), IntToTime(FIeldByName('TimeOut').AsInteger))))
    else
      FieldByName('TotalTime').Value := '??';
    [b]if FieldByName('TotalTime').value <> '??' then
      dblHrsWorked := dblHrsWorked + FieldByName('TotalTime').value;[/b]
  end;
end;

So for the data above, the total hours worked is 27.

The DBGrid is large enough to hold 6 entries. Subsequently the Hours Worked label only sums those first 6 entries. So, for the example above, only the first six entries are summed and the total hours displayed is 25. If I enlarge the DBGrid to fit 10 rows, the total hours display is correct at 27.

I moved the bolded calculation to the OnShow event and it works correctly now, I was just wondering if this is normal.

Thanks for any insight!!!

Les
 
Many types of dataset will only fetch records as it needs to, rather than immediately fetching all appropriate records, so if your dbgrid is only big enough to display 6 records then only the first 6 records will initially be fetched. If you scroll down the grid then the subsequent records will be fetched as necessary.
The OnCalcFields event is triggered each time a record is fetched from the db, and hence your dblHrsWorked field will only contain a total for those records that have been fetched so far.
If you have put the calculation in your OnShow event, then I guess you are iterating through all records of the dataset and hence forcing the dataset to fetch all records, and hence your calculation will be correct.
In summary: yes, I think this is normal!

Steve
 
Also, as an FYI, I've had problems in the past with calculated fields on queries not refreshing correctly when I edit data. If the data you're querying is for display only, then this should not be a problem. However, if you're allowing editing of the data, I would be wary of the calculated field not displaying the correct value.

-Dell

A computer only does what you actually told it to do - not what you thought you told it to do.
 
Thanks for the insight!!

Dell - this is only for display, no editing allowed!!!

Les
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top