?
Solved

ADO AddNew() Function Help

Posted on 2003-03-08
5
Medium Priority
?
363 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
[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
  • 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

Technology Partners: 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

Templates For Beginners Or How To Encourage The Compiler To Work For You Introduction This tutorial is targeted at the reader who is, perhaps, familiar with the basics of C++ but would prefer a little slower introduction to the more ad…
Many modern programming languages support the concept of a property -- a class member that combines characteristics of both a data member and a method.  These are sometimes called "smart fields" because you can add logic that is applied automaticall…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.
Suggested Courses

764 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