CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4
  1. #1
    Join Date
    Nov 2010
    Posts
    3

    [SOLVED]Checking for no return data on query call

    Hey guys, long story short i'm querying an access database for a serial number by a given host name and it is failing to correctly identify when the query returns no match (when the host name is not in the database)

    The following is an excerpt code block that executes the query and checks the returned data:

    Code:
     // Prepare SQL query to grab serial number of host
    //host variable is a _bstr_t that is given a value earlier
    
                    _bstr_t query = "SELECT [SerialNumber] FROM [Computer Inventory] WHERE [ComputerName] = \"" + host + "\";";
    
                    // Excecute the query
                    DAO::RecordsetPtr pRS = NULL;
                    pRS = pDbPtr->OpenRecordset(query, _variant_t(DAO::dbOpenDynaset));
    
                    //create record set, check for bad set
                    if (pRS && 0 < pRS->RecordCount)
                    {
                        DAO::FieldsPtr pFields = NULL;
                        pFields = pRS->GetFields();
                        if (pFields && pFields->Count == 0)
                        {
                            cout<<DAM<<": Error: Number of fields in the result set is 0."<<endl;
    						
                        }
                        // get actual data
                                            //check for VARIANT = VT_NULL and VARIANT = VT_EMPTY
    					if ((pFields->GetItem(0)->GetValue().vt != 1) && (pFields->GetItem(0)->GetValue().vt != 0))
    						serial = (_bstr_t)(pFields->GetItem(0)->GetValue());
    					else{
    						cout << "\nComputer has no record in database!";
    						return "99/99/9999";//return no date identifier (ignore)
    					}
    				}
    I checked for VT_NULL and VT_EMPTY, is their another value the VARIANT could be if the query finds no matches in the database?
    Last edited by entropy01; November 23rd, 2010 at 09:40 AM. Reason: Solved

  2. #2
    Join Date
    Jul 2010
    Posts
    31

    Re: Checking for no return data on query call

    if there is no data returned, it might be lost after casting

  3. #3
    Join Date
    Nov 2010
    Posts
    3

    Re: Checking for no return data on query call

    Thanks it was a good though but I found the mistake...

    This line "if (pRS && 0 < pRS->RecordCount)"

    actually is the first check that concludes there was no data returned, and since there was no accompanying else for that if, there was no action being taken against no results!

    The second check for no data:
    if ((pFields->GetItem(0)->GetValue().vt != 1) && (pFields->GetItem(0)->GetValue().vt != 0))

    Never got evaluated as essentially that case was already checked.

    Thanks guys!

  4. #4
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,396

    Re: Checking for no return data on query call

    I guess you have to do
    Code:
    pRS->MoveLast();
    before getting pRS->RecordCount.
    Victor Nijegorodov

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured