HOWTO: Determine Number of Records Affected by an ADO UPDATE

ID: Q195048


The information in this article applies to:


SUMMARY

The Execute method of the ActiveX Data Objects (ADO) Command object passes by reference an integer value that you can use to retrieve the number of records affected by a SQL UPDATE command.


MORE INFORMATION

The following example establishes a DSN-less connection to the SQL Server sample table ROYSCHED in the PUBS database, executes a SQL UPDATE command, displays the number of records affected by the command, then executes another UPDATE command to restore the ROYSCHED table to its previous state.

NOTE: Modify the SERVER component of the lcConnString variable, the lcUID and lcPWD variables as appropriate for your SQL Server installation.

In order to use this example, you must have Microsoft Data Access Components (MDAC) version 2.x or later installed, which is included in the data components of Visual Studio 6.0 or can be downloaded from the following Web address:

http://www.microsoft.com/data/

Sample Code


   #DEFINE adModeReadWrite 3
   #DEFINE adCmdText 1

   oConnection = CREATEOBJECT("ADODB.Connection")
   oCommand = CREATEOBJECT("ADODB.Command")

   lcConnString = "DRIVER={SQL Server};" + ;
      "SERVER=YourServerName;" + ;
      "DATABASE=pubs"

   lcUID = "YourUserID"
   lcPWD = "YourPassword"

   oConnection.ATTRIBUTES = adModeReadWrite
   oConnection.OPEN(lcConnString, lcUID, lcPWD )

   * Use the command object to perform an UPDATE
   * and return the count of affected records.
   strSQL = "UPDATE roysched SET royalty = royalty * 1.5"
   liRecordsAffected = 0
   WITH oCommand
      .CommandType = adCmdText
      .ActiveConnection = oConnection
      .CommandText = strSQL
      .Execute(@liRecordsAffected)
   ENDWITH
   =MESSAGEBOX("Records affected: " + LTRIM(STR(liRecordsAffected)))

   * Set the royalty column back to its previous value.
   strSQL = "UPDATE roysched SET royalty = royalty / 1.5"
   liRecordsAffected = 0
   WITH oCommand
      .CommandType = adCmdText
      .ActiveConnection = oConnection
      .CommandText = strSQL
      .Execute(@liRecordsAffected)
   ENDWITH

   =MESSAGEBOX("Records affected: " + LTRIM(STR(liRecordsAffected))) 

Additional query words: _TALLY


Keywords          : kbActiveX kbADO200 kbDatabase kbMDAC kbSQL 
Version           : WINDOWS:6.0
Platform          : WINDOWS 
Issue type        : kbhowto 

Last Reviewed: August 8, 1999