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

Finding a database total

Status
Not open for further replies.

AnniHilatE

Programmer
Sep 30, 2006
5
AU
hi, im not all that advanced at using delphi. but i have still completed my senior school level for IT Systems (programming using delphi)

i am currently creating a program for my workplace which will be keeping track of all the stock that gets put upstairs in our storeroom.
im using a standard paradox7 database with fields for codes, descriptions and quantities.
as part of the program, there was a need to create a function to cound the amount of items that were stocked upstairs. so to do this, i figured the easiest way would be to cound the number of entries in the database itself and assign it to a textbox.
simply, click a button, count the entries and display it in a textbox.

i have tried looking through the delphi help for commands that might do it simply, but i couldnt find any...

any help would be greatly appreciated.
cheers
AnniHilatE
 
you can loop through the table and add up the qty's

Code:
var qty:integer;
begin
 qty:=0;
 TableParts.First;
  while not TableParts.Eof do
   begin
    qty:=qty + TableParts.FieldByName('Qty').AsInteger;
    TableParts.Next;
   end;
end;

Aaron Taylor
John Mutch Electronics
 
Use a query
In the SQL property type:
Code:
SELECT COUNT(qty)FROM Testdb
When the query is run or refreshed it will count the quantities. Extract the number and use it in tlabel or on a statusbar.

You can use the SQL-explorer to preview the result.

Steven
 
ok they work great :)
is there a way to just count how many items are in the database. ie: one item is counted once.
say...
DA123 - Table x24
AA333 - Necklace x56
DB634 - Gift Box x15
it will count how many different items listed, not the quantity. so with that example, there would be 3 items counted
 
Code:
  SELECT code, COUNT(code), SUM( qty) FROM Testdb
  GROUP BY code

Steven
 
if you want a count of how many records are in the table the TTable has a RecordCount property

label1.caption:=mytable.recordcount;

Aaron Taylor
John Mutch Electronics
 
well,
label1.caption:=mytable.recordcount;

works like a charm.
and sorry to svanels, i ment that yours was working well

i went to try the loop code, but i keep getting an error saying "liststocktable: Field 'StockQuantity' not found"
although i have double checked that the quantity field is exactly the same as the one it cant find...
 
all the fields in me liststocktable have already been added. this is why im slightly confused why it doesnt pick it up
 
AnniHilatE, could you give the header (fieldnames) of your table, and the values (examples)

DA123 - Table x24
AA333 - Necklace x56
DB634 - Gift Box x15

I am sure that organizing multiple fields can done more efficient wit a query than looping through a table. a query component can be linked to a dbgrid for display.

Your 2nd question..

Steven
 
the main part of the program is all running within the program itself. so i would much rather a loop that having to refer to an sql query. its simply so that it can just count it up and display the result in a textbox on the same form. (unless you can run a query on the same form which the button is pressed and display the result in a textbox)

stockdatabase.db

liststocktable (table)

StockCode, StockDescription, StockQuantity, StockSection (field names in the table)

records:
Code: Description: Qty: Section:

DA2233 A description here 23 1
BA1234 A product here 12 3
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top