Seeking partial strings in CDaoRecordset class
Sometimes while searching for something in CDaoRecordset object we often use Seek() function. But the problem with the DAO is, it reports FALSE when search is made for partial string. For example suppose there is record "Sharad" in the FirstName field of table and it is indexed. If search is made for some prefix only the return value is FALSE as shown here:
COleVariant var("Sha");
Seek("=", &var ) returns FALSE, although "Sha" is prefix of "'Sharad".
The whole point is very relevant when we want to quickly navigate through record set as the user types the key strokes for the item he is searching.
To get around this I have implemented a function called PartialSeek( const char * szStr , const char * szFieldName ) as member of CDaoRecordset. To use this the accompanying source code of this function should be added to your CDaoRecordset derived class :
class CMyRecordset :public CDaoRecordset
{
public:
BOOL PartialSeek(const char * szStr , const char * szFieldName ) ;
} ;
BOOL CMyRecordset::PartialSeek( const char * szStr , const char * szFieldName )
{
int nSeekResult ;
int nStrCmp;
int nLen ;
COleVariant Var(szStr , VT_BSTRT );
COleVariant Extract ;
COleVariant varBookMark ;
nLen = strlen(szStr);
varBookMark = GetBookmark();
nSeekResult = Seek("<", &Var);
MoveNext();
if (IsBOF() ||IsEOF())
{
SetBookmark(varBookMark);
return 0 ;
}
GetFieldValue( szFieldName , Extract) ;
nStrCmp = _strnicmp(szStr, (const char *)Extract.bstrVal, nLen );
if ( nStrCmp == 0)
{
return TRUE ;
}
else
{
SetBookmark(varBookMark);
return FALSE ;
}
}
How to call function:
Suppose the field is FirstName search key is "Sha"
We should make call like this: PartialSeek("Sha" , "FirstName") ;
Prerequisite
(1) This works only with text type of fields:
(2) The field must be indexed.
(3) Before using this function the index should be switched to the field we are looking for, by making a call to SetCurrentIndex() ;
Date Last Updated: April 4, 1999

Comments
Please have an Example!!!
Posted by Legacy on 08/26/2002 12:00amOriginally posted by: Pravin
Replytroubles with seek
Posted by Legacy on 07/11/2001 12:00amOriginally posted by: Borge Pedersen
I am sure it must be the seek function which is at fault. I am searching a small database in Dao. Everything seems to work well if you input a name which does not exist (or part of). Now I have a few with my surname but if I were to input pew, the seek collapses in a heap.
I have a couple of traces using AfxMessageBox and look at the szStr and that is equal to paw, as input. However the output of seek which incidentally is BOOL is 1 for something which does not exist.
I wonder if anyone has had that kind of trouble - there are further notes on the subject in News database and mfc programming but so far nobody has ventured an opinion.dj
ReplyThanks for some good code
Posted by Legacy on 04/09/2000 12:00amOriginally posted by: Borge Pedersen
ReplyWhy not to use SQL instead of Seek?
Posted by Legacy on 09/23/1999 12:00amOriginally posted by: Jaime
ReplyHow to search for other types of the field???
Posted by Legacy on 08/11/1999 12:00amOriginally posted by: To
Is there a source for searching for other type of the field
Replyas numerical data?
Good bit of Code, but doesn't always find the First Record....
Posted by Legacy on 04/15/1999 12:00amOriginally posted by: Wayne Gibson
Reply