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
{

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:00am

    Originally posted by: Pravin

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

    Reply
  • 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 opinion.dj

    Reply
  • 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

    Reply
  • Why not to use SQL instead of Seek?

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

    Originally posted by: Jaime

    Hi..
    
    

    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'";
    pMyRecordSet->Requery();
    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.

    Jaime

    Reply
  • 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?

    Reply
  • 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;
    break;
    }
    MoveNext();
    if(IsEOF())
    {
    break;
    }
    }

    if(!ReturnValue)
    {
    SetBookmark(varBookMark);
    }

    return ReturnValue;
    //// End Of Code

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

    Wayne Gibson

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

Top White Papers and Webcasts

  • On-demand Event Event Date: September 10, 2014 Modern mobile applications connect systems-of-engagement (mobile apps) with systems-of-record (traditional IT) to deliver new and innovative business value. But the lifecycle for development of mobile apps is also new and different. Emerging trends in mobile development call for faster delivery of incremental features, coupled with feedback from the users of the app "in the wild." This loop of continuous delivery and continuous feedback is how the best mobile …

  • Packaged application development teams frequently operate with limited testing environments due to time and labor constraints. By virtualizing the entire application stack, packaged application development teams can deliver business results faster, at higher quality, and with lower risk.

Most Popular Programming Stories

More for Developers

Latest Developer Headlines

RSS Feeds