Using SQL To Find Sum

I have a form that I dropped a TQuery (call it Query1) onto.  I need Query1 to give me the total number of records that meet the certain conditions.  The data is stored in a TTable (call it Table1).  The conditions are:
myId = x    // x can change
myWt = y    // y can change

What would I enter for Query1.Params and Query1.SQL?  What would I type in order to use the result of this query, i.e. total number of records that meet my conditions e.g. ttlRecords := ??? ?  What other things (highly technical term) do I need to set to make the query work?
LVL 6
tecbuilderAsked:
Who is Participating?

[Webinar] Streamline your web hosting managementRegister Today

x
 
Pegasus100397Connect With a Mentor Commented:
In your query:

Select COUNT(field in table) from TABLE where blah blah blah.

Should return the total number of records that meet this criteria.

Good luck with your project!

Pegasus
0
 
tecbuilderAuthor Commented:
Edited text of question.
0
 
tecbuilderAuthor Commented:
Adjusted points to 150
0
Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

 
Pegasus100397Commented:
Tecbuilder, reread your question, try this for your SQL.Text:

Select Count(*) from TABLENAME where
myId = :myIdVariable AND
myWt = :myWtVariable

Make sure the query is closed then:

Query1.ParamByName('myIdVariable').AsInteger := 50;
Query1.ParamByName('myWtVariable').AsInteger := 50;
Query1.Open;
yourrecordcount := Query1.Fields[0].AsInteger;

Good luck with your project!
Pegasus
0
 
tecbuilderAuthor Commented:
Should I use a try - finally block around any of this? If so where should it start and what should go in the finally part?

Thanks for your quick response!
0
 
Pegasus100397Commented:
TecBuilder,

   You *could* put a try..finally block around it (and it's good programming practice, although I would use the Try..EXCEPT statement instead ;) so I'd re-write it like this if you wanted to handle a possible exception:

Query1.ParamByName('myIdVariable').AsInteger := 50;
Query1.ParamByName('myWtVariable').AsInteger := 50;
Try
 Begin
  Query1.Open;
  yourrecordcount := Query1.Fields[0].AsInteger;
 end;
Except
  begin
  ShowMessage('Query OPEN failed! Help!');
  yourrecordcount := 0;
  end;
end;

Good luck and let me know how it works out!
Pegasus
0
 
tecbuilderAuthor Commented:
Thanks for your help!
0
All Courses

From novice to tech pro — start learning today.