PRB: The ActiveX Data Objects Save and Open Methods Example Do Not Work (268740)



The information in this article applies to:

  • ActiveX Data Objects (ADO) 2.1
  • ActiveX Data Objects (ADO) 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 "More Information" 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.

MORE INFORMATION

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 "Berkeley." This is the original value of the field. To view the change, change the value to something other than "Berkeley."
  • Visual Basic

    1. Change the SELECT statement in the Recordset.open call to "SELECT * FROM authors" in the SaveX1() procedure.
    2. 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=<username> and Password =<strong 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 "SELECT * FROM Authors", _
             "Provider=SQLOLEDB;Data Source=MySrvr;User Id=<username>;" & _
             "Password=<strong password>;Initial Catalog=pubs;", _
             adOpenDynamic, adLockOptimistic, adCmdText
    						
  • Visual C++

    1. 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;
      								
    2. 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("a:\\Pubs.xml","Provider=MSPersist;",
                  adOpenForwardOnly,adLockBatchOptimistic,adCmdFile);
      							
  • Visual J++

    1. 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=<username> and Password =<strong 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 = "Provider=SQLOLEDB;Data Source=srv;Initial Catalog=Pubs;User Id=<username>;Password=<strong password>;";
      								
    2. Change the Recordset.open() statement in SaveX2() to the following:
       rstAuthors.open("a:\\Pubs.xml",
                     "Provider=MSPersist;",
                     AdoEnums.CursorType.FORWARDONLY,
                     AdoEnums.LockType.BATCHOPTIMISTIC,
                     AdoEnums.CommandType.FILE);
      								
    3. Change the Recordset.open() statement in SaveX3() to open the correct file as demonstrated in the following code:
      rstAuthors.open("a:\\Pubs.adtg");
      								

Modification Type:MajorLast Reviewed:11/4/2003
Keywords:kbMSXMLnosweep kbprb KB268740 kbAudDeveloper