Solved

excel vba oledb wrong value for recordsaffected

Posted on 2013-12-12
5
385 Views
Last Modified: 2014-01-19
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
Comment
Question by:chow_cheska
  • 3
5 Comments
 
LVL 28

Expert Comment

by:omgang
ID: 39715452
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
 

Accepted Solution

by:
chow_cheska earned 0 total points
ID: 39716300
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
 
LVL 10

Expert Comment

by:broro183
ID: 39716779
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
 

Author Comment

by:chow_cheska
ID: 39723382
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
 

Author Closing Comment

by:chow_cheska
ID: 39791917
there was no answer - only a work-around
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Introduction This Article briefly covers methods of calculating the NPV and IRR variants in Excel as well as the limitations in calculating and interpreting IRR results. Paraphrasing Richard Shockley, author of my favourite finance reference tex…
Improved? Move/Copy Add-in Replacement - How to avoid the annoying, “A formula or sheet you want to move or copy contains the name XXX, which already exists on the destination worksheet.” David Miller (dlmille)  It was one of those days… I wa…
The viewer will learn how to create a normally distributed random variable in Excel, use a normal distribution to simulate the return on an investment over a period of years, Create a Monte Carlo simulation using a normal random variable, and calcul…
The viewer will learn how to create two correlated normally distributed random variables in Excel, use a normal distribution to simulate the return on different levels of investment in each of the two funds over a period of ten years, and, create a …

760 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now