Well you may be working too hard, for example you don't need a Connection, a Command, and a Recordset here. It can all be done with one ADO Command object.
You also don't want to use the Open method, use Execute instead. Execute takes three parameters, the first being
RecordsAffected, the second
Parameters, and the third
Options. Here we only need the first and third ones.
Hmmm...
Set up a test "database"
I don't have a SQL Server database handy, and no Access on this machine either. Here's a quick example using Jet to update a named range of cells in an Excel workbook. The Excel/Jet combo is just simulating a database to be updated via ADO:
* I created a workbook called MyWorkBook.xls
* I have one worksheet called MyWorkSheet
* I set up MyWorkSheet as follows. The {x} symbols below represent the row and column IDs of the table cells. We can ignore these for our purposes.
Code:
{A} {B}
{1} Resident Age
{2} Fred 32
{3} Wilma 29
{4} Barney 32
{5} Betty 27
* I created a named cell range covering A1 - B5 and called it Bedrock.
This gives us a simulated database MyWorkBook.xls with a "table" called Bedrock having two columns named Resident and Age.
Write an update script
I want a script that will find every row with Age = 32 and change the Age to 34, then show me the number of rows that were updated.
I'll call it ADOCmd.wsf because by writing a Windows Script File instead of a naked .vbs file I can use the <reference> and <object> elements. The first will give me access to the ADO constants w/o having to define them myself. The second will let me declare the Command object without messing about with a Dim and a CreateObject( ) and a Set ... = Nothing and all that jazz.
You can do this the regular way (as a .vbs) but you'll need to look up and define the ADO constants then.
Run the script
We run the script and out comes our message:
Then we can open the workbook MyWorkbook.xls in Excel and we see:
Code:
{A} {B}
{1} Resident Age
{2} Fred 34
{3} Wilma 29
{4} Barney 34
{5} Betty 27
Just as we wanted, expected, and saw reported: 2 rows were updated.
Listing of ADOCmd.wsf
Here is the listing:
Code:
<job id="ADOCmd">
<reference object="ADODB.Command" />
<object id="objCmd" progid="ADODB.Command" />
<script language="VBScript">
Option Explicit
Dim lngRecordsAffected
objCmd.ActiveConnection = "Provider = Microsoft.Jet.OLEDB.4.0;" _
& "Data Source = MyWorkBook.xls;" _
& "Extended Properties = Excel 8.0;"
objCmd.CommandText = "UPDATE Bedrock SET Age = 34 WHERE Age = 32"
objCmd.Execute lngRecordsAffected, Null, adCmdText And adExecuteNoRecords
WScript.Echo "Updated " & CStr(lngRecordsAffected) & " rows"
</script>
</job>
Note the use of Null for the second Execute method parameter (whch we don't need in this case, thus I pass Null). Note also the
Options mask passed as the third parameter. Here I've said "the CommandText property is not a file name or URL, etc. but is indeed the text of a command to execute" and also "don't return any records from this Execute call."
Hope this is clear and gives you what you need. It should work just fine with your SQL Server databases. Execute is documented at: