tags:

views:

47

answers:

2

Hi I am running VB with the follwing piece of code

conider strSQL to be a update string .

conDB.Execute (strSQL) ;
Sheet1.Cells(intStart, 5) = "Success"

If there are no rows updated , the below lines still runs and i get a success message printed in the cell. How do i avoid this?

A: 

The problem is that your Success message is displayed regardless of whether or not your Update actually updated.

I believe you can call the conDB.RecordsAffected function to return the number of records that were updated. Check the return value of this function and display the appropriate message.

Try something like this:

conDB.Execute (strSQL)
If (conDB.RecordsAffected > 0) Then
    Sheet1.Cells(intStart, 5) = "Success"
Else
    Sheet1.Cells(intStart, 5) = "Failed"
End If
Jay Riggs
I get the following error.Arguments are of the wrong type, are out of acceptable range, or are in conflict with one another.
misguided
@misguided - I might have blown it on the type returned by RecordsAffected (I don't have access to a machine with VB/VBA). I updated, let us know if that works.
Jay Riggs
Thanks Jay . But I still get the same error "Arguments are of the wrong type, are out of acceptable range, or are in conflict with one another."
misguided
@mis - On which line are you getting the error?
Jay Riggs
Getting error on {code} If (conDB.RecordsAffected() > 0) Then {code}I beleive it has something to do with working on conDB , because before asking this question I was trying {code}conDB.Execute(strSQL).RecordCount{code} which was throwing a similar error.
misguided
Do you really have a semicolon after your first line there?
Jay Riggs
@misguided: That could be because you typed `conDB.RecordsAffected()` as against `conDB.RecordsAffected`. You are calling it as if `RecordsAffected` is a method whereas it is a property. Remove the `()` after `RecordsAffected`.
shahkalpesh
@ Jay No, i did not give the semicolon. Is it required?@shahkalpesh tried it without () still the same error.
misguided
+1  A: 
dim recordsAffected as long

conDB.Execute strSQL, recordsAffected 
Sheet1.Cells(intStart, 5) = IIF(recordsAffected  > 0, "Success", "Failure")
shahkalpesh
Hi , conDB.Execute(strSQL,recordsAffected) gives a syntax error. Also FYI Dim conDB As New ADODB.Connection
misguided
@misguided: Remove the brackets from the line containing `Execute`. See the modified code above.
shahkalpesh
it did resolve the issue . I have accepted the anwer. Thanks.
misguided