Click here to Skip to main content
13,005,488 members (61,322 online)
Click here to Skip to main content
Add your own
alternative version


15 bookmarked
Posted 16 Aug 2004

A simple count() query class

, 10 Jun 2005
Rate this:
Please Sign up or sign in to vote.
A class to easily do count() queries.


This simple class uses the basic CRecordset to implement count() queries. It's not too hard to do it by issuing SQL statements, it's not necessarily that hard to catch the return value. This class just makes it easier.


I wrote this as part of a much larger database application. I've re-used the code several times since, so I figured it must be of use to others, too.

Using the code

To use the code, just add the code and header files to your project. You need to edit one line in countset.cpp:

CString CCountSet::GetDefaultConnect()
    return _T("ODBC;DSN=WinTAS");  // put your own DSN in here

Then just #include countset.h in the header file you want to use it in, and set it up as per the example:

CCountSet countSet;
countSet.sSelect="some SQL statement returning a numeric value, 
    such as MAX(age)";  // delete this line to use the next one
countSet.sDistinct="field name for select(distinct <fieldname>) 
    statement";  // delete this line for select(*);
countSet.sCheckTable="table name you're counting in"; 
// this is just appended to the SQL,
// so could include JOIN statements

countSet.sCheckField="WHERE condition for SQL statement";
// delete for no WHERE clause

// never Requery() it: Close() and Open() if you want to re-use it.

// return value is in countSet.m_TOTAL;

So, there it is - declare an instance of CCountSet, set some variables, and you're away. For example, to generate the SQL statement count(max(age)) from client, you'd write:

CCountSet countSet;
CString sText;

sText.Format("The oldest client is %d",countSet.m_TOTAL);


This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


About the Author

Paul S Ganney
Web Developer
United Kingdom United Kingdom
No Biography provided

You may also be interested in...

Comments and Discussions

GeneralThanks! Pin
Eugene Plokhov18-Oct-07 0:42
memberEugene Plokhov18-Oct-07 0:42 
GeneralCRecordset Pin
viva dotnet18-Oct-04 6:34
memberviva dotnet18-Oct-04 6:34 
GeneralRe: CRecordset Pin
Paul S Ganney18-Oct-04 6:49
memberPaul S Ganney18-Oct-04 6:49 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.170627.1 | Last Updated 10 Jun 2005
Article Copyright 2004 by Paul S Ganney
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid