Link to home
Start Free TrialLog in
Avatar of chow_cheska
chow_cheska

asked on

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 ..?
Avatar of omgang
omgang
Flag of United States of America image

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
ASKER CERTIFIED SOLUTION
Avatar of chow_cheska
chow_cheska

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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
Avatar of chow_cheska
chow_cheska

ASKER

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!
there was no answer - only a work-around