# I need a field to enter minutes that calculates every 60 minutes = 1 hour, etc.

I have a data entry form that users need to enter in their Approx. Time Spent (in minutes).  For example, they work on an item for 10 minutes, I want them to just have to enter in "10" but if they worked on something for an hour and a half, I want them to enter "90".  The data entered into this form I will be showing on a grid and I want the grid to compute that the total for those 2 items, "10" minutes and "90" minutes, was a total of 2 Hours and 40 minutes.

Any help is appreciated!
###### Who is Participating?

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Commented:
Are you looking for a function to convert minutes to hours/minutes???

``````function MinutesToHours(Minutes: Integer): string;
var
Hours: Integer;
Minutes: Integer;
begin
if (Minutes > 0) then begin
Hours   := Minutes div 60;
Minutes := Minutes mod 60;
end
else begin
Hours   := 0;
Minutes := 0;
end;

Result := Format('%.2d:%.2d', [Hours, Minutes]);
end;
``````
0
Network EngineerAuthor Commented:
Hmm... That might work. I can make my data entry field a regular Text field that users enter in minutes, then the grid can calculate the field.... I will try that now.
0
Commented:
There are two ways you can do it, either loop the dataset or get it straight from the database

Here are the codes
``````Method 1 - Get from database

procedure TForm1.Button1Click(Sender: TObject);
//Edit1.Text has the user name
var
Total: Integer;
begin
try
Q.SQL.Add(Format('SELECT SUM([Time Spent]) CNT FROM TABLE1 WHERE USER = %s', [Edit1.Text]));
Q.Active := True;
Total := Q.FieldByName('CNT').Asinteger;
Q.Active := False;
finally
FreeAndNil(Q);
end;
Label1.Caption := IntToStr(Total DIV 60) + ' Hrs ' + IntToStr(Total MOD 60) + ' Mins';
end;

Method 2 - loop through Dataset
procedure TForm1.Button2Click(Sender: TObject);
var
Total: Integer;
begin
Total := 0;
begin
Total := Total + ADOQuery1.FieldByName('[Time Spent]').AsInteger;
end;

Label1.Caption := IntToStr(Total DIV 60) + ' Hrs ' + IntToStr(Total MOD 60) + ' Mins';
end;
``````
0
Commented:
If you mean DBGrid then you can add calculated field of the Type String, in your DB component:

``````procedure TForm1.AdoTable1CalcFields(DataSet: TDataSet);
begin
end;
``````
0

Experts Exchange Solution brought to you by

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Commented:
Sorry typo, here is update:
``````procedure TForm1.AdoTable1CalcFields(DataSet: TDataSet);
begin
end;
``````

BTW:
10 minutes and 90 minutes = 1 hours and 40 minutes
0
Network EngineerAuthor Commented:
Yes, its a DBGrid. I now have 3 items, 2 with "10" and 1 with "40" pulling from the table and showing in the TimeSpent field on the grid. How can I now add the sums in the group/footer to sum with the calculations and show 'Hours' and 'Minutes'? What event do I link it to? I have formatted percentages before using the OnGetDisplayText but that just changes how it shows within the cell, not for the group/footer sums.
Thanks!
0
Commented:
Create a new calc column "Time" in the DB component (as attached) and link your DBGrid intended column to this "column "Time" and then add the above code at OnCalcFields of that same DB Component:
``````procedure TForm1.AdoTable1CalcFields(DataSet: TDataSet);
begin
end;
``````
CalcField.JPG
0
Commented:

Are you using DevExpress grid
0
Network EngineerAuthor Commented:
Yes, DevExpress Grid
0
Commented:

Got to the DataController property of the TableView, expand it and you can create summary fields from there.

Select the column and field name then specify Sum as the calculation

Summary.PNG
SummarySetupField.bmp
0
Network EngineerAuthor Commented:
I have done that before for normal counts and sums but in this case, I need to know how to format the sum so that anything over 60 minutes shows as '1 Hour' so if the total sum is 90, the sum at the group or footer shows "1 Hour, 30 minutes"
0
Commented:
Its better to set it up with the properties but if you are adventorous and want to do it in code, you use the following code
``````with (AView as TcxGridDBTableView) do
with DataController.Summary do
begin
BeginUpdate;
try
begin
Column := Columns[2]; //index of the column you are summing
Kind := skSum;
end;
finally
EndUpdate;
end;
end;
``````
0
Network EngineerAuthor Commented:
that works but how do I format it to divide out the minutes by 60 and show it as 1 Hour?
0
Commented:

Let me see, I'll check it out
0
Commented:
Use the GetText event like this
``````procedure TForm1.cxGrid1DBTableView1TcxGridDBDataControllerTcxDataSummaryFooterSummaryItems0GetText(
Sender: TcxDataSummaryItem; const AValue: Variant; AIsFooter: Boolean;
var AText: String);
begin
if not VarIsNull(AValue) then
AText := Format('%d: %d', [AValue DIV 60, AValue MOD 60]);
end;
``````
0
Commented:

Just make sure you are using the GetText event of the summary field and not of the column

``````procedure TForm2.cxGrid1DBTableView1TcxGridDBDataControllerTcxDataSummaryDefaultGroupSummaryItems0GetText(
Sender: TcxDataSummaryItem; const AValue: Variant; AIsFooter: Boolean;
var AText: string);
begin
if not VarIsNull(AValue) then
AText := Format('%d:%d', [AValue DIV 60, AValue MOD 60]);
end;

or

procedure TForm1.cxGrid1DBTableView1TcxGridDBDataControllerTcxDataSummaryFooterSummaryItems0GetText(
Sender: TcxDataSummaryItem; const AValue: Variant; AIsFooter: Boolean;
var AText: String);
begin
if not VarIsNull(AValue) then
AText := Format('%d %d', [AValue DIV 60, AValue MOD 60]);
end;
``````
Summary.png
0
Commented:
I have provided the solution using the GetText method. This is how custom formating is done in devexpress fields and its the way I handle it in my applications
0
Commented:

Are you kidding?

I just gave you the correct answer, how does adding a calculated field to each row give you summary
0
Commented:

Yes I did,

The accepted solution makes no sense, where as the Author is asking about formating the summary field and I quote "grid to compute that the total for those 2 items"
Creating a calculated field (as accepted here) works on each row but produces no summary. to get the summary, you actually have to create a summary field for the grid.

I use devexpress grid all the time and I know that for sure, So I just wondered how that could have been the correct solution.
0
Oracle dbaCommented:
Prec94513
You may find that any solution given for DBGrid does not work for devexpress grids