Solved

Searching record fields using ODBC VC++ 4.0

Posted on 1998-11-09
8
381 Views
Last Modified: 2010-04-02
I'm trying to figure out how to do a search in an ODBC application (VC++4.0). Unlike the MFC DAO database classes, the MFC ODBC recordset class does not have a Find member function. Say I have five fields per record, and want to search all the fields in each record until I find the string I am looking for. Assuming there is a command, how do I do the loop?? Do I use the m_pSet in the view that points to the record set? If so, how exactly do I go about it? If you have any sample code please feel free to supply a copy. Thanks!
0
Comment
Question by:pigeonbr
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 4
8 Comments
 
LVL 4

Expert Comment

by:pagladasu
ID: 1177417
Are you sure you want to search all the fields in each record for the string, or is it just one particular field. If it is all fields, then they must all be of string type.
0
 

Author Comment

by:pigeonbr
ID: 1177418
Hi again:)  Yes, I do realize the fact they must all be strings, and that like I said, there is no Find member function using ODBC. Just point me in the right direction if you can, and I will work at it some more. Can I use the m_pSet pointer to access each field? I tried this but to no avail. I tried using the form m_pSet->[field] , field being ant int from 0 to4 to cover my 5 fields, but does not work...I'm just looking for the technique to use. I know how to use MoveFirst() to move to the next record, but it's to access the fields once I'm in the record. I want to compare the fields in the record to the variable I get from a search edit box from which I enter the desired string to find. If I don't find a match, I move to the next record, and so on, until I reach EOF, at which point I have a MessageBox come up to tell me there is no match. I will appreciate all help you can give me on this one. Thanks! Have a good day!

0
 
LVL 4

Accepted Solution

by:
pagladasu earned 100 total points
ID: 1177419
Here are some hints to you.
The total no. of fields is obtained by accessing the CRecordset member m_nFields,
something like
int nrec=m_pSet->m_nFields;
The CRecordset class has a member function called GetFieldValue. One version of it is like this
void GetFieldValue( short nIndex, CString& strValue );
where nIndex is a zero based index of the field number, and strValue is the string that will finally contain the value of that field.
So first go to first record.
While there are still records-
For each rcord:
Loop thru the fields and use this funtion to retrieve the field values.
Check its value with your search string.
If there is a match - you've got it - now do whatever you want.
If not continue

I guess I have been able to clarify properly.
Thanks and best of luck
pagladasu
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:pigeonbr
ID: 1177420
Hi:) Just studying your answer this evening, and when I look for the GetFieldValue() function in the ehlp, I notice it is a function that supports DAO. If you take a look at my original question, I did state that I was using ODBC, and that was my problem. Guess you didn't catch that. The reason there was no function in ODBC was the purpose of the question, to find a way of doing the search in each record. I guess the only way is to create a function, but could you give me some guidelines? Thanks:)
0
 
LVL 4

Expert Comment

by:pagladasu
ID: 1177421
Sorry, I just found it. Are you referring to using ODBC API's to find?
GetFieldValue() works with both DAO and ODBC recordsets.
0
 

Author Comment

by:pigeonbr
ID: 1177422
void CPiecesCLRView::OnGoCheckBox()
{      
      //Transfer find edit box string to variable
      UpdateData(TRUE);      
      CString strValue;
      int fieldindex;
      //Move to the first record
      m_pSet->MoveFirst();
      int nfields = m_pSet->m_nFields;
      
            for(fieldindex=0;fieldindex<nfields;fieldindex++)
            {
                  GetFieldValue(fieldindex,strValue);
                  //Compare string variable with edit box data variable
                  if(strValue == m_SearchEditBox)
                  {
                        //Display current record
                        UpdateData(FALSE);
                        MessageBeep((WORD)-1);
                        break;
                  }
            }
      
            m_pSet->MoveNext();
            //If we moved off the end of file, move back to the last record
      if(m_pSet->IsEOF())
            m_pSet->MoveLast();

            
      return;
}

               Hi! This is a copy of my code trying to use GetFieldValue. In my help, it is referred to in the CDaoRecordset, and not the CRecordset...I'm using VC++ ver 4.0, standard edition...when I compile the program, I get an undeclared identifier error at the line i use the GetFieldValue ()...Am I doing something wrong, or as I said, it does seem that in my case, the function is not supported by ODBC...??


0
 

Author Comment

by:pigeonbr
ID: 1177423
Another pertinant question while I'm at it:)) Is there a function to get the number of records in a CRecordset?? nFields works fine for the fields, but I can't find a function to get the # of records. If not, I guess i juste keep moving to the next record, and check if EOF, right? Thanks!

0
 
LVL 4

Expert Comment

by:pagladasu
ID: 1177424
OK. I get your problem. VC++ 4 does not support GetFieldValue() as a member function of CRecordSet class - VC++ 5 however has a CRecordSet member function with this name. I use Version 5; I tested it out there before posting the answer.
Just give some time to think - I may figure out a way for you.
About total no of records - whether its VC++ or VB, there is a big problem. Do a MoveFirst() followed by a MoveLast() and then use the function GetRecordCount().
Thanks
pagladasu
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction This article is the first in a series of articles about the C/C++ Visual Studio Express debugger.  It provides a quick start guide in using the debugger. Part 2 focuses on additional topics in breakpoints.  Lastly, Part 3 focuses on th…
This article will show you some of the more useful Standard Template Library (STL) algorithms through the use of working examples.  You will learn about how these algorithms fit into the STL architecture, how they work with STL containers, and why t…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

696 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question