Microsoft KB Archive/268740

= PRB: The ActiveX Data Objects Save and Open Methods Example Do Not Work =

Article ID: 268740

Article Last Modified on 11/4/2003

-

APPLIES TO


 * Microsoft ActiveX Data Objects 2.1
 * Microsoft ActiveX Data Objects 2.5

-



This article was previously published under Q268740



SYMPTOMS
The ActiveX Data Objects (ADO) Save and Open Methods example in Microsoft Visual C++, Microsoft Visual J++, and Microsoft Visual Basic that were published in the Microsoft Developer Network (MSDN), contain several errors that cause them not to work. The programs do not make any changes to the database, as they are designed to, and the Visual Basic and Visual C++ versions contain syntax errors.



CAUSE
This behavior occurs because both the Visual C++ and Visual J++ samples do not open the disconnected recordset in batch update mode. This is required to later save the data to the database. The Visual Basic sample does not select the City field from the Authors table. The City field is later updated and causes an error.



RESOLUTION
To resolve this behavior, load the recordset in batch update mode by using adLockBatchOptimistic. For more details, please see the &quot;More Information&quot; section of this article.

In the Visual Basic sample, make the modifications shown to remove the syntax error, and to select all fields from the Authors table.

In the Visual C++ sample make the modifications shown to remove the syntax error.



Steps to Reproduce Behavior
Run the samples as shown in MSDN. The Visual Basic and Visual C++ samples do not compile or run. Even after you correct the syntax errors, none of the samples make any changes to the database.

Corrections
 ==== All versions (Visual Basic, Visual C++, Visual J++) ====

In SaveX2, the City field is updated to &quot;Berkeley.&quot; This is the original value of the field. To view the change, change the value to something other than &quot;Berkeley.&quot;  ==== Visual Basic ====

 Change the SELECT statement in the Recordset.open call to &quot;SELECT * FROM authors&quot; in the SaveX1 procedure. Insert a comma on the third line of the Recordset.open call before the underscore (_) symbol in the SaveX1 procedure.

The Recordset.open call should look like the following:

Note You must change User ID= and Password = to the correct values before you run this code. Make sure that User ID has the appropriate permissions to perform this operation on the database. rst.Open &quot;SELECT * FROM Authors&quot;, _ &quot;Provider=SQLOLEDB;Data Source=MySrvr;User Id= ;&quot; & _ &quot;Password= ;Initial Catalog=pubs;&quot;, _ adOpenDynamic, adLockOptimistic, adCmdText  ==== Visual C++ ====

  Change the first statement in the main function so that it is not missing a closing parenthesis. The statement should look like the following: if(FAILED(::CoInitialize(NULL))) return; </li>  Change the Recordset.open call in SaveX2 so that it opens the recordset in batch optimistic mode as demonstrated in the following code: pRstAuthors->Open(&quot;a:\\Pubs.xml&quot;,&quot;Provider=MSPersist;&quot;,           adOpenForwardOnly,adLockBatchOptimistic,adCmdFile); </li></ol> </li> ==== Visual J++ ====

  Change the connect strings in SaveX1 and SaveX3 to use the Microsoft OLE DB Provider for SQL Server instead of the Microsoft OLE DB Provider for ODBC drivers as demonstrated in the following code:

Note You must change User ID= and Password = to the correct values before you run this code. Make sure that User ID has the appropriate permissions to perform this operation on the database. String strCnn = &quot;Provider=SQLOLEDB;Data Source=srv;Initial Catalog=Pubs;User Id= ;Password= ;&quot;; </li>  Change the Recordset.open statement in SaveX2 to the following: rstAuthors.open(&quot;a:\\Pubs.xml&quot;,              &quot;Provider=MSPersist;&quot;,               AdoEnums.CursorType.FORWARDONLY,               AdoEnums.LockType.BATCHOPTIMISTIC,               AdoEnums.CommandType.FILE); </li>  Change the Recordset.open statement in SaveX3() to open the correct file as demonstrated in the following code: rstAuthors.open(&quot;a:\\Pubs.adtg&quot;); </li></ol> </li></ul>

Additional query words: platform sdk sample code snippet

Keywords: kbprb kbmsxmlnosweep KB268740

-

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

© Microsoft Corporation. All rights reserved.