Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

ADO AddNew() Function Help

Posted on 2003-03-08
5
Medium Priority
?
374 Views
Last Modified: 2007-12-19
I am trying to use the AddNew() function to add a record to a MS Access database.  I want to use pure api, not mfc.  Here is the code I have - it gets a runtime error when the AddNew function is called.

_bstr_t string;
char buffer[255];

if(CustomerInfo_type == 0)
{
     string = "Customers";
     Data->pRecordset->Open(string,
               Data->pConnection.GetInterfacePtr(),
               adOpenForwardOnly,
               adLockOptimistic,
               adCmdTable);
     
     _variant_t FieldList[16];
        _variant_t ValueList[16];
     _variant_t FieldIndex;
       
     FieldsPtr fields = Data->pRecordset->GetFields();
         
     FieldPtr pfield;
       
     for(int c = 1; c < 16; c++)
     {                                
           
          FieldIndex.lVal = (long)c;
          pfield = fields->GetItem(FieldIndex);
          FieldList[c] = (_variant_t)pfield->GetName();        
          ValueList[c] = "test";
               
     }
       

     Data->pRecordset->AddNew(FieldList, ValueList);
     
     Data->pRecordset->Close();

}
0
Comment
Question by:eamarks
  • 3
  • 2
5 Comments
 
LVL 4

Expert Comment

by:Chase707
ID: 8096504
Use the try...catch mechanism of c++ to catch the com_error that gets thrown by _RecordsetPtr

try
{
    Data->pRecordset->AddNew(FieldList, ValueList);
}
catch (_com_error e)
{
    cerr << (const char*) e.Description() << endl;
}

that will tell you the error description of the error.

There is another issue:

   for(int c = 1; c < 16; c++)

should be:

   for(int c = 0; c < 16; c++)


Just a few suggestions

Chase707
0
 

Author Comment

by:eamarks
ID: 8099466
Chase707

I added this code, but the MessageBox doesn't show up - just the runtime error.

try
{
               Data->pRecordset->AddNew(FieldList, ValueList);
}
catch(_com_error e)
{
     MessageBox(NULL, (const char*)e.Description(), "", 1);
}
0
 
LVL 4

Expert Comment

by:Chase707
ID: 8099956
Sorry, I've never used AddNew with the argument list.  I did a little research and found out that AddNew takes a Variant as a SafeArray.

You need to create a SafeArray of variants for each parameter to AddNew -- a normal array will not work.

Let me know if you need help on creating and passing a SafeArray as a parameter.

Another thing you can do is check the return value:

HRESULT hr = Data->pRecordset->AddNew(FieldList, ValueList);

if (FAILED(hr))
{
   char szErrNum[10];
   sprintf(szErrNum, "%i", hr);
   MessageBox(NULL, szErrNum, "", MB_OK);
}


It will give you an error code that you can look up as well.

0
 

Author Comment

by:eamarks
ID: 8100695
I've seen MFC code where they use COleSafeArray but I don't know how to use a safearray in api.
0
 
LVL 4

Accepted Solution

by:
Chase707 earned 280 total points
ID: 8106768
I've gone ahead and grabbed some old code I had for creating a safe array and added it to your code that you posted.  I haven't tested or compiled this, but it should get you headed in the right direction.

You can see that the key functions are:
SafeArrayCreate,
SafeArrayPutElement,
SafeArrayDestroy

You can look these up on MSDN, along with SAFEARRAYBOUND for more info.
         
_variant_t varFields;   // declare the variant
_variant_t varValues;   // declare the variant

varFields.vt = VT_ARRAY | VT_VARIANT;   // it is an array of variant
varFields.parray = NULL; // initalize the array pointer

varValues.vt = VT_ARRAY | VT_VARIANT;   // it is an array of variant
varValues.parray = NULL; // initalize the array pointer

SAFEARRAY *psaArrayFields = 0;       // the fields array
SAFEARRAY *psaArrayValues = 0;       // the values array
SAFEARRAYBOUND rgsabound[1];         // the array bounds
         
rgsabound[0].lLbound = 0;      // the lower bound
rgsabound[0].cElements = 16;  // the num elements
         
psaArrayFields = SafeArrayCreate(VT_VARIANT, 1, rgsabound);   // create the array
psaArrayValues = SafeArrayCreate(VT_VARIANT, 1, rgsabound);   // create the array

 FieldIndex.vt = VT_I4;                              

 // loop through and add values
 for(int c = 0; c < 16; c++)
 {            
    FieldIndex.lVal = (long)c;
    pfield = fields->GetItem(FieldIndex);

    _variant_t varField(pfield->GetName());  // create a field name variant
    _variant_t varValue("test");             // create a field value variant

    SafeArrayPutElement(psaArrayFields, (long*) &c, &varField);  // put the field in the array
    SafeArrayPutElement(psaArrayValues, (long*) &c, &varValues); // put the value in the array
 }

varFields->parray = psaArrayFields;               // assign the variant pointer
varValues->parray = psaArrayValues;               // assign the variant pointer

try
{
   // add the record
   HRESULT hr =Data->pRecordset->AddNew(varFields, varValues);

   if (FAILED(hr))
   {
     char szErrNum[10];
     sprintf(szErrNum, "%i", hr);
      MessageBox(NULL, szErrNum, "Hresult", MB_OK);
   }
}
catch (_com_error e)
{
    MessageBox(NULL, (const char*)e.Description(), "exception", 1);
}


// destroy the safe arrays
SafeArrayDestroy(psaArrayFields);
SafeArrayDestroy(psaArrayValues);
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Article by: SunnyDark
This article's goal is to present you with an easy to use XML wrapper for C++ and also present some interesting techniques that you might use with MS C++. The reason I built this class is to ease the pain of using XML files with C++, since there is…
Article by: evilrix
Looking for a way to avoid searching through large data sets for data that doesn't exist? A Bloom Filter might be what you need. This data structure is a probabilistic filter that allows you to avoid unnecessary searches when you know the data defin…
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.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

581 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