Solved

ADO and Aggregate Fields

Posted on 2006-06-24
5
1,019 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

This article explains how to create forms/units independent of other forms/units object names in a delphi project. Have you ever created a form for user input in a Delphi project and then had the need to have that same form in a other Delphi proj…
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…
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, just open a new email message. In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
Both in life and business – not all partnerships are created equal. As the demand for cloud services increases, so do the number of self-proclaimed cloud partners. Asking the right questions up front in the partnership, will enable both parties …

861 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

Need Help in Real-Time?

Connect with top rated Experts

25 Experts available now in Live!

Get 1:1 Help Now