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 derfloh on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Calculated field...Newbie Alert 2

Status
Not open for further replies.

xxman

Technical User
Jun 1, 2004
80
US
HI

I have a access database linked with AdoTable. I have dbedits for data entry and a navigator linked to a dbgrid. In the grid there are corresponding fields that show the data in the dbedits. I wanted to add a new field as a calculated field. This field would add data from two other fields and place the total in the calculated field column of the dbgrid. Do I use byfieldname for the calculated field equation. As you might guess this is my first attempt. Any code samples would be greatly appreicated!!!!!
 
Since you're using a calculated field, I'm assuming that you've got persistent fields set up (double-click on your ADOTable, right-click in the window that appears and select Add All Fields, then also add your calculated field - you MUST do this in order to use a calculated field.) This means that you can do your calculated field either with "FieldByName" or with the persistent field object itself.

In the OnCalcFields event handler for the table, you'll do something like the following:
Code:
if MyTableFIELD1.IsNull then
begin
  if MyTableFIELD2.IsNull then
    MyTableCALCULATEFIELD.Value := 0
  else
    MyTableCALCULATEFIELD.Value := MyTableFIELD2.Value;
end
else
  MyTableCALCULATEFIELD.Value := MyTableFIELD1.Value + 
    MyTableFIELD2.Value;

Using FieldByName, you'd do something like this:
Code:
  MyTable.FieldByName('CALCULATEFIELD').AsInteger :=
     MyTable.FieldBYName('FIELD1').AsInteger + 
     MyTable.FieldByName('FIELD2').AsInteger;

Using the persistent fields is a little faster, using FieldByName is simpler code.

-D
 
Hi hilfy

Thanks so much. You should write reference books, you
make more sense then some of the books I`ve pondered over!

You`ve been a great help!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
 
Hi

I just noticed a problem. If I edit the dbedits the only
time the value updates is if I page back and forth thru
the records. How is it possible to update on a current record without moving to another record. Hope I`m clear on this. Would I refresh as the last line in the code

Thanks
 
Delphi needs to know when you want any changes to your fields actually written to the database (otherwise it would be updating records after every keystroke).

There are two ways this can be done.

Either by scrolling to another record which implies that you want the changes written to the database

OR

by explicitly asking the application to write the changes. This can be done by clicking on the tick of the DBNavigator component if it exists or by having a button with a caption with something like 'Update'. The Update button should call the Post procedure for the dataset.

Andrew
Hampshire, UK
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top