Microsoft Knowledge Base Email Alertz

This step-by-step article describes how use Microsoft Visual C++ to call the ADO

Search KbAlertz

Advanced Search

Receive Microsoft Knowledge Base articles by E-Mail?

Every night we scan the Microsoft Knowledge Base. If technologies you're interested in are updated, we'll send you an e-mail. You only get one e-mail a day, and only when new articles are added.

Click here to create a
FREE account
Already have an account?
[Click here to Login]











Microsoft Knowledge Base Article

This article contents is Microsoft Copyrighted material.
©2005-©2007 Microsoft Corporation. All rights reserved. Terms of Use | Trademarks

Article ID: 310078 - Last Review: May 10, 2003 - Revision: 2.1

HOW TO: Call ADO AddNew Method with an Array of Fields and Values by Using Visual C++

This article was previously published under Q310078

On This Page

SUMMARY

This step-by-step article describes how use Microsoft Visual C++ to call the ADO AddNew method by using an array of fields and values.

Call the AddNew Method

The ADO Recordset object contains a method called AddNew that is used to add a record to a recordset. The AddNew method takes the following 2 optional parameters:
  • An array of field names or ordinal positions.
  • An array of values for each of the fields.
The documentation for the AddNew method shows how to call AddNew by using the ADO Visual C++ extensions (IADORecordBinding), but does not demonstrate how to call AddNew by using the optional parameters. If you want to call AddNew with an array of fields and values, the parameters must be a SAFEARRAY of VARIANTs (VT_VARIANT). A common mistake is to pass a SAFEARRAY array of BSTRs (VT_BSTR).

The following sample code shows how to call the AddNew method by using the optional parameters:


#include <stdio.h>
#include <iostream>
using namespace std;

#pragma warning(disable:4146)

#undef EOF

#import "c:\program files\common files\system\ado\msado15.dll" no_namespace 

#define MAX_FIELDS 5

int main(int argc, char* argv[])

{
    _RecordsetPtr pRs;

    CoInitialize(NULL);

    pRs.CreateInstance(__uuidof(Recordset));

    TCHAR szColName[64];
	
    // Define a SafeArray that contains field names.
    // - SafeArray of Variants
    SAFEARRAY * psaFields; 
    SAFEARRAYBOUND aDimFields[1]; 
    aDimFields[0].lLbound = 0; 
    aDimFields[0].cElements = MAX_FIELDS;

    psaFields = SafeArrayCreate(VT_VARIANT, 1, aDimFields); 

    long ix[1];
    _variant_t var;

    for(int i = 0; i < MAX_FIELDS; i++)
    {
        ix[0] = i;
	sprintf(szColName, "Col_%d", i);   
	var = szColName;

        // Add a field name to the SafeArray.
        SafeArrayPutElement(psaFields, ix, (void*) (VARIANT *) (&var));

        // Add a field to the recordset.
	pRs->Fields->Append(szColName, adVarChar, 100, adFldUnspecified); 

     }


     // Open the recordset.
     pRs->Open(vtMissing, vtMissing, adOpenStatic, adLockOptimistic, adCmdUnspecified);

     // Create a SafeArray for the field values.
     SAFEARRAY * psaValues;
     SAFEARRAYBOUND aDimValues[1];
     aDimValues[0].lLbound = 0;
     aDimValues[0].cElements = MAX_FIELDS;
     psaValues = SafeArrayCreate(VT_VARIANT, 1, aDimValues);
	
     // Populate the SafeArray of values.
     TCHAR szValue[100];

     for(int lVal = 0; lVal < MAX_FIELDS; lVal++)
     {   
         ix[0] = lVal;
         sprintf(szValue, "VALUE%d", lVal);
         var = szValue;
         SafeArrayPutElement(psaValues, ix, (void*)(VARIANT *) &var);
     }

               
     // Define VARIANTS that are SafeArrays of VARIANTS.
     _variant_t vtFields, vtValues;
     vtFields.vt = VT_ARRAY | VT_VARIANT;
     vtValues.vt = VT_ARRAY | VT_VARIANT;
     vtFields.parray = psaFields;
     vtValues.parray = psaValues;

     // Add 10 records.
     for(int k = 0; k < 10; k++)
        pRs->AddNew(vtFields, vtValues);

     // Set to the first record and dump the results of the recordset.
     pRs->MoveFirst();

     for (int cRow=0; cRow < 10;cRow++)
     {
         for (int cCol=0; cCol < MAX_FIELDS; cCol++)
         {
           cout << (TCHAR *)_bstr_t((pRs->Fields->Item[(long)cCol]->Value)) <<"    ";
         }

         cout << endl;
     }
     return 0;
}
				

APPLIES TO
  • Microsoft Data Access Components 2.6
  • Microsoft Data Access Components 2.7
  • Microsoft ActiveX Data Objects 2.6
  • Microsoft ActiveX Data Objects 2.7
Keywords: 
kbhowto kbhowtomaster kbmdacnosweep KB310078
       

Community Feedback System

Very often, it takes hours to solve a problem. Very often, you've looked high and low, and have tried a lot of solutions. When you finally found it, chances are, it was because someone else helped you. Here's your chance to give back. Use our community feedback tool to let others know what worked for you and what didn't.

Please also understand that the community feedback system is not warranted to be correct, it's simply a system that we've built to let people try and help each other. If something in a feedback response doesn't make sense to you, or you're not comfortable making changes that the feedback talks about (like registry edits), please consult a professional.

Thank you for using kbAlertz.com Feedback System.

-- Scott Cate