Notice: This website is an unofficial Microsoft Knowledge Base (hereinafter KB) archive and is intended to provide a reliable access to deleted content from Microsoft KB. All KB articles are owned by Microsoft Corporation. Read full disclaimer for more details.

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


View products that this article applies to.

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;
}
				

↑ Back to the top


Keywords: KB310078, kbmdacnosweep, kbhowtomaster, kbhowto

↑ Back to the top

Article Info
Article ID : 310078
Revision : 3
Created on : 5/10/2003
Published on : 5/10/2003
Exists online : False
Views : 518