Microsoft KB Archive/310078

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

Article ID: 310078

Article Last Modified on 5/10/2003

-

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

-



This article was previously published under Q310078



IN THIS TASK

 * SUMMARY
 * Call the AddNew Method



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.

back to the top

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:

using namespace std;
 * 1) include 
 * 2) include


 * 1) pragma warning(disable:4146)


 * 1) undef EOF


 * 1) import &quot;c:\program files\common files\system\ado\msado15.dll&quot; no_namespace


 * 1) 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, &quot;Col_%d&quot;, 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, &quot;VALUE%d&quot;, 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)) <<&quot;    &quot;; }

cout << endl; }    return 0; } back to the top

Additional query words: safearray

Keywords: kbhowto kbhowtomaster kbmdacnosweep KB310078

-

[mailto:TECHNET@MICROSOFT.COM Send feedback to Microsoft]

© Microsoft Corporation. All rights reserved.