Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

ADO and Aggregate Fields

Posted on 2006-06-24
5
Medium Priority
?
1,058 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 150 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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

Creating an auto free TStringList The TStringList is a basic and frequently used object in Delphi. On many occasions, you may want to create a temporary list, process some items in the list and be done with the list. In such cases, you have to…
Introduction I have seen many questions in this Delphi topic area where queries in threads are needed or suggested. I know bumped into a similar need. This article will address some of the concepts when dealing with a multithreaded delphi database…
This Micro Tutorial will teach you how to add a cinematic look to any film or video out there. There are very few simple steps that you will follow to do so. This will be demonstrated using Adobe Premiere Pro CS6.
We’ve all felt that sense of false security before—locking down external access to a database or component and feeling like we’ve done all we need to do to secure company data. But that feeling is fleeting. Attacks these days can happen in many w…
Suggested Courses

877 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