Microsoft Knowledge Base Article
This article contents is Microsoft Copyrighted material.
©2005-©2007 Microsoft Corporation. All rights reserved.
Terms
of Use |
Trademarks
Article ID: 195048 - Last Review: July 1, 2004 - Revision: 4.3
How To Determine Number of Records Affected by an ADO UPDATE
This article was previously published under Q195048
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.
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:
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)))
APPLIES TO
- Microsoft Visual FoxPro 6.0 Professional Edition
- Microsoft Data Access Components 2.1 Service Pack 2
- Microsoft Data Access Components 2.5
- Microsoft Data Access Components 2.6
| kbdatabase kbhowto kbsqlprog KB195048 |
Community Feedback System
Very often, it takes hours to solve a problem. Very often, you've looked high
and low, and have tried a lot of solutions. When you finally found it, chances
are, it was because someone else helped you. Here's your chance to give back.
Use our community feedback tool to let others know what worked for you and what
didn't.
Please also understand that the community feedback system is not warranted to be
correct, it's simply a system that we've built to let people try and help each
other. If something in a feedback response doesn't make sense to you, or you're
not comfortable making changes that the feedback talks about (like registry
edits), please consult a professional.
Thank you for using kbAlertz.com Feedback System.
-- Scott Cate