• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 536
  • Last Modified:

excel vba oledb wrong value for recordsaffected

Hello,

I have a very simple problem which I have spent 2 days googling for an answer, to no help.

I an receiving a wrong value back for recordsaffected from a batabase connection execute ...

it seems that once the lngRecsAff is set, it stays the same value regardless of success or fail from each successive execution.

(I am of course expecting a 0 in no record was created and a 1 if it was)

Dim cn As ADODB.Connection
Dim strSQL As String
Dim lngRecsAff As Long
Set cn = New ADODB.Connection
    cn.Open _
     "Provider = sqloledb;" & _
     "Data Source=WSP0470.datasource.com;" & _
     "Initial Catalog=QTS;" & _
     "User ID=user;" & _
     "Password=me;"

For i = 2 To lastrow
    strSQL = "INSERT INTO .."
    
    cn.Execute strSQL, lngRecsAff, adExecuteNoRecords
    
    If lngRecsAff = 0 Then 'there was a problem and no record was added  
       qtyRowsFailed = qtyRowsFailed + 1
    Else 'all cool
        qtyRowsAddedToDatabase = qtyRowsAddedToDatabase + lngRecsAff
    End If

Next i

Open in new window



even if I were to set lngRecsAff = 5, it just stays that value though the entire loop ..?
0
chow_cheska
Asked:
chow_cheska
  • 3
1 Solution
 
omgangIT ManagerCommented:
See  http://msdn.microsoft.com/en-us/library/windows/desktop/ms675023(v=vs.85).aspx

From what I get out of that I'm wondering if you need to assign the call to a recordset
<<<Using the Execute method on a Connection Object (ADO) object executes whatever query you pass to the method in the CommandText argument on the specified connection. If the CommandText argument specifies a row-returning query, any results that the execution generates are stored in a new Recordset object. If the command is not intended to return results (for example, an SQL UPDATE query) the provider returns Nothing as long as the option adExecuteNoRecords is specified; otherwise Execute returns a closed Recordset.
>>>

Dim rs As ADODB.Recordset

Set rs = cn.Execute strSQL, lngRecsAff, adExecuteNoRecords


or, have you already tried this?
OM Gang
0
 
chow_cheskaAuthor Commented:
thanks, but I actually tried that also ... same result. - wierd .. I was starting to think it was because it's not acccess?

I just tried this and this seems to be a work-around, I haven't tested enough yet ...

Debug.Print strSQL
cn.Execute strSQL, lngRecsAff, adExecuteNoRecords


'total possible rows into batabase (regardless of success fail)
qtyTotalRows = qtyTotalRows + 1

'fail / success counts
errorcount = cn.Errors.count 'were there any errors?
If errorcount > 0 Then 'yes there were errors
    wehavefailedrows = "yes"
    qtyRowsFailed = qtyRowsFailed + 1
Else 'no there were no errors
    qtyRowsSuccess = qtyRowsSuccess + 1
End If

Open in new window


it seems to be ok, and I can even throw the cn.Errors.Item into an array if I want to keep track of them for the user ...
0
 
broro183Commented:
hi,

The way this code is written it appeared (to my out of practice eye) as if no value is ever assigned to lngRecsAff & that would be why it doesn't change. However I then read up on the "connection.execute" method in the MS Access 2007 Help Files which gave me more understanding. In the Help Files it stated:
For best performance in a Microsoft Access workspace, especially in a multiuser environment, nest the Execute method inside a transaction. Use the BeginTrans method on the current Workspace object, then use the Execute method, and complete the transaction by using the CommitTrans method on the Workspace. This saves changes on disk and frees any locks placed while the query is running.

Can you modify your code to use the relevant Workspace object & see if using CommitTrans within the loop results in the lngRecsAff variable being updated the next time that the .execute method is used?

hth
Rob
0
 
chow_cheskaAuthor Commented:
Thanks Rob.

i did also try assigning lngRecsAff = 0 after the declaration, but that didn't make a difference at all ...

If I get some time I will certainly try the nested committrans .. this will never be used multiuser however, and actually, catching the errror message can help later if someone changes something and there are problems.

Apprecaite the help!
0
 
chow_cheskaAuthor Commented:
there was no answer - only a work-around
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now