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?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

tecbuilderAuthor Commented:
Edited text of question.
0
tecbuilderAuthor Commented:
Adjusted points to 150
0
Pegasus100397Commented:
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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Cloud Class® Course: CompTIA Cloud+

The CompTIA Cloud+ Basic training course will teach you about cloud concepts and models, data storage, networking, and network infrastructure.

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
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Delphi

From novice to tech pro — start learning today.