-
October 18th, 2005, 11:37 AM
#1
To count records in a mdb file
VC++ 6.0
MFC
I need to know the number of records in a table of a .mdb Access 97 file.
The code I have uses CRecordset (CRecordset* m_pSet)
Now I'm using a loop to count records
Code:
int n=0;
m_pSet->MoveFirst();
while (m_pSet->IsEOF()==FALSE){
n++;
m_pSet->MoveNext();
}
With a big number of records this method is not very efficient.
¿Do anybody know other method more efficient to retrieve the number of records?
Thank you
-
October 18th, 2005, 11:52 AM
#2
Re: To count records in a mdb file
It looks as though you are using DAO to access the database here.
Switching to ADO would have significant benefits, including the ability to perform a GetRecordCount on a recordset.
An alternative might be to query the database directly:
SELECT Count(TableID) FROM tblMyTable WHERE xxxx;
...which returns the number of rows/records matching your query.
Hope this helps,
- Nigel
-
October 18th, 2005, 11:55 AM
#3
Re: To count records in a mdb file
Unfortunately there isn't a way to retrieve the max number of records directly (I don't know personally.). You have to continue with using MoveNext() until EOF, only then can you use GetRecordCount().
Can I suggest that you use SQL to acheive this?
Code:
SELECT COUNT(*) FROM <tablename>
Regards
John
I don't mind that you think slowly but I do mind that you are publishing faster than you think. Wolfgang Pauli, physicist, Nobel laureate (1900-1958)
-
October 18th, 2005, 12:01 PM
#4
Re: To count records in a mdb file
Originally Posted by NigelQ
It looks as though you are using DAO to access the database here.
Switching to ADO would have significant benefits, including the ability to perform a GetRecordCount on a recordset.
An alternative might be to query the database directly:
SELECT Count(TableID) FROM tblMyTable WHERE xxxx;
...which returns the number of rows/records matching your query.
Hope this helps,
- Nigel
The ADO option would imply lot of changes. I think is inaccessible now.
And I can't figure out how to query the database directly and obtain a response with tne number of records.
Thank you
-
October 18th, 2005, 12:05 PM
#5
Re: To count records in a mdb file
Originally Posted by Vaderman
Unfortunately there isn't a way to retrieve the max number of records directly (I don't know personally.). You have to continue with using MoveNext() until EOF, only then can you use GetRecordCount().
Can I suggest that you use SQL to acheive this?
Code:
SELECT COUNT(*) FROM <tablename>
Regards
John
Yes, but where is stored the number of records?
I can call ExecuteSQL() but the return is void
Thank you
-
October 18th, 2005, 12:12 PM
#6
Re: To count records in a mdb file
Here's a function I wrote to do it. You can modify it as necessary.
Code:
int GetCount(CRecordset* pSet, const char* lpszUnique)
{
if(pSet->IsOpen())
{
if(pSet->IsEOF())
{
CRecordsetStatus rStat;
pSet->GetStatus(rStat);
if(rStat.m_bRecordCountFinal == 0)
return 0;
}
}
AfxGetApp()->BeginWaitCursor();
SQLHSTMT hstmt;
SQLRETURN retcode;
SQLINTEGER iIndCount;
long lValue;
retcode = ::SQLAllocStmt(pSet->m_pDatabase->m_hdbc, &hstmt);
SQLRETURN retVal = SQLBindCol(hstmt, 1, SQL_C_SLONG, &lValue, 0, &iIndCount);
CString strSQL;
if(lpszUnique != NULL)
strSQL.Format("SELECT COUNT(DISTINCT [%s]) FROM %s", lpszUnique, pSet->GetTableName());
else
strSQL.Format("SELECT COUNT(*) FROM %s", pSet->GetTableName());
if(!pSet->m_strFilter.IsEmpty())
{
strSQL += " WHERE ";
strSQL += pSet->m_strFilter;
}
retVal = SQLExecDirect(hstmt, (SQLTCHAR*)(const char*)strSQL, SQL_NTS);
if(retVal == SQL_ERROR)
{
return -1;
}
while ((retcode == SQL_SUCCESS) || (retcode == SQL_SUCCESS_WITH_INFO))
retcode = SQLFetch(hstmt);
SQLFreeStmt(hstmt, SQL_CLOSE);
SQLFreeStmt(hstmt, SQL_DROP);
return lValue;
}
-
October 18th, 2005, 12:36 PM
#7
Re: To count records in a mdb file
Originally Posted by GCDEF
Here's a function I wrote to do it. You can modify it as necessary.
Your method is really fast, I have improved the speed totally.
Thank you very much.
-
October 18th, 2005, 12:38 PM
#8
Re: To count records in a mdb file
Originally Posted by Doctor Luz
Your method is really fast, I have improved the speed totally.
Thank you very much.
Wow, you got it implemented quickly.
Happy to help.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|