Solved

Searching record fields using ODBC VC++ 4.0

Posted on 1998-11-09
8
359 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
  • 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
 

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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
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

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

C++ Properties One feature missing from standard C++ that you will find in many other Object Oriented Programming languages is something called a Property (http://www.experts-exchange.com/Programming/Languages/CPP/A_3912-Object-Properties-in-C.ht…
Container Orchestration platforms empower organizations to scale their apps at an exceptional rate. This is the reason numerous innovation-driven companies are moving apps to an appropriated datacenter wide platform that empowers them to scale at a …
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.

707 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now