Solved

ADO and Aggregate Fields

Posted on 2006-06-24
5
1,015 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
Hi,

Thank you.

Regards, Geo
0

Featured Post

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

Suggested Solutions

A lot of questions regard threads in Delphi.   One of the more specific questions is how to show progress of the thread.   Updating a progressbar from inside a thread is a mistake. A solution to this would be to send a synchronized message to the…
Hello everybody This Article will show you how to validate number with TEdit control, What's the TEdit control? TEdit is a standard Windows edit control on a form, it allows to user to write, read and copy/paste single line of text. Usua…
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

763 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

13 Experts available now in Live!

Get 1:1 Help Now