Solved

excel vba oledb wrong value for recordsaffected

Posted on 2013-12-12
5
432 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

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…
Introduction While answering a recent question (http:/Q_27311462.html), I created an alternative function to the Excel Concatenate() function that you might find useful.  I tested several solutions and share the results in this article as well as t…
This Micro Tutorial demonstrates how to create Excel charts: column, area, line, bar, and scatter charts. Formatting tips are provided as well.
This Micro Tutorial demonstrates in Microsoft Excel how to consolidate your marketing data by creating an interactive charts using form controls. This creates cool drop-downs for viewers of your chart to choose from.

734 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