Solved

ADO and Aggregate Fields

Posted on 2006-06-24
5
1,025 Views
Last Modified: 2008-01-09
Hi,

I would like to display an aggregate field using ADO.

I have an ADOTable which is the 'Item' table in my DB with primary key the "Item No". I also have another table in my DB called 'Item Ledger Entry' containing the fields "Item No", "Quantity" and primary key is the "Entry No".

I would like to display in a DBEdit the Sum of "Quantity" from the 'Item Ledger Entry' group by "Item No". My form displays the 'Item' table so all I want to do is to add this DBEdit displaying the sum of quantity for information.

Which is the quickest way to achieve this? I have thought of creating a ADOQuery having as Parameter the "Item No" which will be obtained by the DBEdit displaying this field from the 'Item' table however I am not sure this is the best way. Could u pls help me with this?
0
Comment
Question by:mc94051
  • 3
  • 2
5 Comments
 
LVL 17

Expert Comment

by:geobul
ID: 16975428
Hi,

I would go the same way. And the query would be something like:

SELECT Sum(Quantity) FROM 'Item Ledger Entry' WHERE 'Item No' = :Param;

You may add an event handler with the query above in Item table OnScroll or if you're displaying it record by record (not in a grid) in a next/prev button click.

Regards, Geo
0
 

Author Comment

by:mc94051
ID: 16975818
Thanks Geo. I thought there was a way to avoid the query and all the fuss.
Actually my code is the one following however it does not work with the message "delphi parameter object is improperly defined". Any ideas?

  anADOQuery := TADOQuery.Create(Self);
  anADOQuery.Connection := DBConnection;
  anADOQuery.Parameters.CreateParameter('SelectedItemNo', ftString , pdInput, 10, Null);
  anADOQuery.Parameters.ParamByName('SelectedItemNo').Value := DBEdit1.Text;
  anADOQuery.SQL.BeginUpdate;
  try
    anADOQuery.SQL.Clear;
    anADOQuery.SQL.Add('SELECT [Item Ledger Entry].[Item No_], Sum([Item Ledger Entry].Quantity) AS SumOfQuantity ');
    anADOQuery.SQL.Add('FROM [Item Ledger Entry] ');
    anADOQuery.SQL.Add('GROUP BY [Item Ledger Entry].[Item No_] ');
    anADOQuery.SQL.Add('HAVING ((([Item Ledger Entry].[Item No_]) =: SelectedItemNo))');
  finally
    anADOQuery.SQL.EndUpdate;
  end;
  anADOQuery.Open;

0
 
LVL 17

Accepted Solution

by:
geobul earned 50 total points
ID: 17052114
Hi,

I cannot test anything right now but:

= :SelectedItemNo))');

there was a space between the : and the name of the parameter.

Regards, Geo
0
 

Author Comment

by:mc94051
ID: 17059210
Hi,

This is the solution but I had already found it days ago. Anyway, I 'll award you with the points since you spent your time on this
0
 
LVL 17

Expert Comment

by:geobul
ID: 17073641
Hi,

Thank you.

Regards, Geo
0

Featured Post

Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In this tutorial I will show you how to use the Windows Speech API in Delphi. I will only cover basic functions such as text to speech and controlling the speed of the speech. SAPI Installation First you need to install the SAPI type library, th…
Introduction Raise your hands if you were as upset with FireMonkey as I was when I discovered that there was no TListview.  I use TListView in almost all of my applications I've written, and I was not going to compromise by resorting to TStringGrid…
This Micro Tutorial will give you a basic overview how to record your screen with Microsoft Expression Encoder. This program is still free and open for the public to download. This will be demonstrated using Microsoft Expression Encoder 4.
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…

773 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question