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

How to add two dbedits and post to a dbgrid 2

Status
Not open for further replies.

Mezzzo

Technical User
May 30, 2003
56
US
Hi

I`ve been experimenting with ado and the dbcontrols. I managed to get my sample connection working. I`m wondering how to apply calculations that add the the contents of two dbedit controls and post the results to a field in a dbgrid.
I`m just looking for a bit of code to clarify this process.
Your help is greatly appreciated

DataSource1
ADOConnection1
ADOTable1
MSAccess
 
The key is to work with the TFields, not the TDBEdit and TDBTable.

Like this:

with ADOTable1 do begin
Edit;
FieldbyName('Value').asCurrency:=FieldByName('Price').asCurrency * FieldByName('Quantity).asInteger;
Post;
end;

Have fun
Simon
 
It is not clear from your question as to whether you want to post your calculated field to your Table or simply have it appear in your dbgrid.

It is usually better not to have calculated fields posted to your table from a data normalisation standpoint.

This is how you create a calculated field called Sum which will be calculated from adding Amount1 and Amount2 in your table called Table1.

1. Double click on your Table1 component.

2. Press Ctrl + N

3. Choose the Calculated Field radio button

4. Enter 'Sum' in the Name edit box

5. Enter an appropriate Field Type (such as Currency)

6. Click OK

7. Select the Table component and bring up the Events tab of the Object Inspector.

8. Create a OnCalcFields event handler

9. In the event handler code something like:
Code:
  Table1Sum.AsCurrency := Table1Amount1.AsCurrency + Table1Amount2.AsCurrency;
10. Add the Sum field into your DBGrid.

The Sum field will be calculated as and when necessary although it will never actually be written to the physical database table.

Andrew
 
Hi

Thanks for the information. I`m wondering if the techniques you mentioned would be recommended for the app that I`m attempting. The app is a drawer cutlist program.
The user enters the size of the cabinet opening then many functions are applied to various dbedits. The part calculations for a type of drawer are listed 3 to 6 rows in a grid at a time. The user then enters a new opening and the grid is populated again under the previous data. I had a programmer help with a similar app in Visual basic and he used sql to open and close data from a MSAccess. This was a complete app that didn`t use any bound controls. I`m a beginner at this but it seems that it might be easier to use unbound controls. This would eliminate the use of byfield etc. I`d appreciate your thoughts. To this novice the database implementation seems to be the most confusing part of programming
I appreciated your comprehensive help on my first question.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top