There is a significant error in the code sample posted above - the 'Do Until .EOF' block is missing a ".movenext' statement so you'll be stuck in an infinite loop because the recordset pointer is never moving to the next record, so it will never get to the end and exit the loop.
Additionally, its always good practice to check that the connection to the database was successfully opened (or not) by checking the connection state before proceeding to iterate the recordset:
Code:
if (rs.State = adStateOpen) then
With rs
Do Until .EOF
count1 = count1 + 1
.movenext '<--- if this line is missing, we get stuck in an endless loop.
Loop
End With
else
MsgBox "Could not open a connection to the database." '<-- sadface
end if
Here is a better way to get the affected rows count by using the 'Command' object instead, as follows:
Code:
Dim cnn As ADODB.Connection
Dim cmd as ADODB.Command
Dim totalAffected as long
' instantiate the connection object
Set cnn = New ADODB.Connection
' Open a connection to the database (for example, using the ODBC driver)
cnn.ConnectionString = "driver={SQL Server};server=MySqlServer;uid=MyUserName;pwd=MyPassword;database=pubs"
cnn.Open
if (cnn.State = adStateOpen) then
' instantiate a Command object to handle SQL statements or storedprocs
Set cmd = New ADODB.Command
' set its properties
With cmd
.ActiveConnection = cnn
.CommandType = adCmdStoredProcedure
.CommandText = YOUR_STOREDPROCEDURE_NAME_GOES_HERE
' note:
' we're not dealing with any stored proc parameters in this example.
' leaving that as an exercise for the reader to figure out
.Execute RecordsAffected:=totalAffected
End With
MsgBox "(" & totalAffected & " row(s) affected)"
Set cmd = Nothing
else
MsgBox "Hmm ... Could not open a connection to the database - check the connection string, and that the DB is running", "Connection failed :("
end if
Set cnn = Nothing
I left out robust error handling cos I'm lazy.
Hope that helps