Seeking partial strings in CDaoRecordset class

This article is related to searching character strings in text type of fields of CDaoRecordset only. Please note that it is not applicable to other types of field data types of DAO such as dates or numbers.

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


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);
if (IsBOF() ||IsEOF())
        return 0 ;
GetFieldValue(  szFieldName , Extract) ;
nStrCmp = _strnicmp(szStr, (const char *)Extract.bstrVal, nLen );
if ( nStrCmp == 0)
    return TRUE ;
    return FALSE ;


How to call function:

Suppose the field is FirstName search key is "Sha"

We should make call like this: PartialSeek("Sha" , "FirstName") ;




(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


  • Please have an Example!!!

    Posted by Legacy on 08/26/2002 12:00am

    Originally posted by: Pravin

    The article looks nice but it would be a great help if working example with source code is given....

  • troubles with seek

    Posted by Legacy on 07/11/2001 12:00am

    Originally 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

  • Thanks for some good code

    Posted by Legacy on 04/09/2000 12:00am

    Originally posted by: Borge Pedersen

    I see that I once made a comment about Assert errors. I am happy to say that I have sorted that and have a chunk of code that works extremely well, all thanks to the author. Having messed the code about a little I can now get listings of people the way the expert do it - ie. Input the first letter and I have a listing of all the people whose name start with that letter. As I keep inputting letter I get a more refined, instantaneous search for the correct name.

    Thanks a lot

  • Why not to use SQL instead of Seek?

    Posted by Legacy on 09/23/1999 12:00am

    Originally posted by: Jaime


    Why do you use Seek to make the search? I use SQL in my programs instead.. for example, if pMyRecordset is a DAO recordset object, by doing:

    pMyRecordSet->m_strFilter = "LEFT(FirstName, 3) = 'Sha'";
    if( pMyRecordSet->GetRecordCount( ) > 0 )
    // bla bla

    That's all.. it works for me.. the only requirement is that FirstName has to be of Text Type.


  • How to search for other types of the field???

    Posted by Legacy on 08/11/1999 12:00am

    Originally posted by: To

    Is there a source for searching for other type of the field
    as numerical data?

  • Good bit of Code, but doesn't always find the First Record....

    Posted by Legacy on 04/15/1999 12:00am

    Originally posted by: Wayne Gibson

    I implemented you code for searching in a contact database, but I noticed that it wasn't finding the first
    one if the record was first in the index.  

    So I made the following changes :

    //// Start Of Code
    nSeekResult = Seek(">", &var);
    if(IsBOF() || IsEOF())
    SetBookmark(var BookMark);
    return 0;

    int Loop=0;
    BOOL ReturnValue=FALSE;
    for(Loop=0; Loop < 2; Loop++)
    GetFieldValue(szFieldName, Extract);
    nstrCmp = strnicmp( szStr, (const char *) ExtractbstrVal, nLen);
    if(nStrCmp == 0)
    ReturnValue = TRUE;


    return ReturnValue;
    //// End Of Code

    The reason for checking before moving next is that first record might meet the Criteria.

    Wayne Gibson

Leave a Comment
  • Your email address will not be published. All fields are required.

Top White Papers and Webcasts

  • Moving from an on-premises environment to Office 365 does not remove the need to plan for disruptions or reduce the business risk requirements for protecting email services. If anything, some risks increase with a move to the cloud. Read how to ease the transition every business faces if considering or already migrating to cloud email. This white paper discusses: Setting expectations when migrating to Office 365 Understanding the implications of relying solely on Exchange Online security Necessary archiving …

  • Enterprises are increasingly looking to platform as a service (PaaS) to lower their costs and speed their time to market for new applications. Developing, deploying, and managing applications in the cloud eliminates the time and expense of managing a physical infrastructure to support them. PaaS offerings must deliver additional long-term benefits, such as a lower total cost of ownership (TCO), rapid scalability, and ease of integration, all while providing robust security and availability. This report …

Most Popular Programming Stories

More for Developers

RSS Feeds

Thanks for your registration, follow us on our social networks to keep up-to-date