• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 538
  • Last Modified:

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
0
eugene007
Asked:
eugene007
1 Solution
 
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
 
eugene007Author Commented:
Thanks V_Bapat for the tips on how to calculate the number of records in a recordset.

Cheerio and god bless~
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

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

Tackle projects and never again get stuck behind a technical roadblock.
Join Now