Counting records

Hi all,

I have a DBCtrlGrid connected to an ADO table. It shows a list of customer names. Beside each name, I have a check-box (fieldname is SELECTED).
As I select each customer, I would like to be able to see how many I have selected.

I know I can use ADOTable.Recordcount to count ALL records, but how can I count only the ones with SELECTED=True?

Thanks,
Freerider.
FreeriderAsked:
Who is Participating?
 
chengjianConnect With a Mentor Commented:
function CountRecord(AConnection:TADOConnection):Integer;
var
 aq:TADOQuery;
begin
 Result := -1;
 aq := TADOQuery.Create;
 try
  aq.Connection := AConnection;
  aq.sql.Add('SELECT COUNT(FieldName) FROM YourTable WHERE TheField = TRUE');
  aq.ExecSQL;
  Result := aq.Fields[0].AsInteger;
 finally
  aq.free;
 end;
end;
0
 
CrazyOneCommented:
You could use a query

SELECT Count(TheField) AS CountOfField
FROM TheTable
WHERE TheField = True;


The Crazy One
0
 
CrazyOneCommented:
If the database does not fully support boolean expersions in the WHERE clause you could do this

WHERE TheField <> 0;
0
Get expert help—faster!

Need expert help—fast? Use the Help Bell for personalized assistance getting answers to your important questions.

 
FreeriderAuthor Commented:
CrazyOne,

I haven't used SQL much. How do I get the query into my code?
0
 
CrazyOneCommented:
Well one way is to drop an ADO query on the form. And then double click on the SQL property and then add the SQL statement there. Activate the query componenet like you do the table component.
0
 
chengjianCommented:
sorry,

> aq.ExecSQL;
Should be aq.Open;
0
 
chengjianCommented:
The paramenterAConnection is the TADOConnection component you have used in your program, ADOConnection1 for example.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.