Recordset

CDatabase db;
db.Open("db1", FALSE, FALSE, "ODBC;",
TRUE );
CRecordset rs(&db);
CString m="SELECT * FROM [table1]";
rs.Open( CRecordset::forwardOnly,_T(m));
short nFields = rs.GetODBCFieldCount();
short nFields2=nFields;
char str[10];
sprintf(str,"%d",nFields2);
AfxMessageBox(str);
rs.Close();
db.Close();

Environment : I am using Visual C++ to developed my front end application. I am using ODBC as my interface and i'm using MS Access as my database.

Problem : The program above keeps giving me the total number of records found in the recordset. What i have learn about GetODBCFieldCount is it retrieves the number of records found in your recordset.

Question : How do i get the number of records in the recorset based on a criteria. For instance : In the recordset there are rows containing name like John and etc. If lets say there is five people with the name John. How do i get the value 5 from the recordset.

Regards

Eugene
eugene007Asked:
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.

V_BapatCommented:
Since you are using Access database, it would be better to use MFC DAO classes. But if you want to use ODBC, you can count the no of records yourself.

>What i have learn about GetODBCFieldCount is it retrieves the number of records found in your recordset.
GetODBCFieldCount() gives you the no of *fields* in the recordset and not no of records.

You can count the no of records yourself. Ex:

CDatabase db;
long lCount = 0;
db.Open("db1", FALSE, FALSE, "ODBC;",
TRUE );
CRecordset rs(&db);
CString m="SELECT * FROM [table1]";
rs.Open( CRecordset::forwardOnly,_T(m));
rs.MoveFirst();
while(!rs.IsEOF())
{
lCount++;
rs.MoveNext();
}
CString str("");
str.Format("%d", lCount);
AfxMessageBox("No of records = " + str);

rs.Close();
db.Close();

Alternatively, derive a class from CRecordset and override GetRecordCount to implement this.
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
eugene007Author Commented:
Thanks V_Bapat for the tips on how to calculate the number of records in a recordset.

Cheerio and god bless~
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
System Programming

From novice to tech pro — start learning today.