Automation Error Using Excel to Update SQL DB

All,

I am receiving a "Run-time error '-2147217843 (80040e4d)':  Automation Error" when I try to run the attached code.  Basically I am trying to update muliple records without having to do each one individually using Excel connecting to our SQL DB.

Can someone tell me what is wrong with this?  It's the first time I have tried to use a For...Each statement to update something.

I have tried adding the CurrentProject.Connection.Execute strSQL but it gives me the same error.
Sub Asset_Tag_Update()

Set conSQL = New ADODB.Connection
conSQL.Open "Server=OURSQLSERVER;DRIVER=SQL Server;Database=maverick;User ID=mylogin;Password=mypassword"


For Each Cell In Range("rt_ser")
rt_serial = ActiveCell.Value
new_asset_tag = ActiveCell.Offset(0, 1).Value

rt_serial = "'" & rt_serial & "'"
new_asset_tag = "'" & new_asset_tag & "'"

strSQL = "update audit_item ai" & _
" set new_asset_tag = " & new_asset_tag & "" & _
" where rt_serial = " & rt_serial & ""



Next Cell


End Sub

Open in new window

LVL 7
Brad Sims, CCNANetwork AdministratorAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

sanket_1985Commented:
try after changing

conSQL.Open "Server=OURSQLSERVER;DRIVER=SQL Server;Database=maverick;User ID=mylogin;Password=mypassword"

to

conSQL.Open "Source=OURSQLSERVER;DRIVER=SQL Server;Initial Catalog=maverick;User ID=mylogin;Password=mypassword"

probably it should work.
0
Brad Sims, CCNANetwork AdministratorAuthor Commented:
I tried the change but no luck.  I still get the same error.
0
aikimarkCommented:
Cell is a reserved word and should not be used as a variable.
Create a cellThing variable.

Force variable declaration.

In your SQL statement, you aliased the table as "ai", but you did not qualify any of the fields with that alias.
Option Explicit

Sub Asset_Tag_Update()
  Dim cellThing As Range
  Dim conSQL As ADODB.Connection

  Set conSQL = New ADODB.Connection
  conSQL.Open "Server=OURSQLSERVER;DRIVER=SQL Server;Database=maverick;User ID=mylogin;Password=mypassword"
  
  For Each cellThing In Range("rt_ser")
    rt_serial = cellThing.Value
    new_asset_tag = cellThing.Offset(0, 1).Value
  
    rt_serial = "'" & rt_serial & "'"
    new_asset_tag = "'" & new_asset_tag & "'"

    strSQL = "update audit_item" & _
        " set new_asset_tag = " & new_asset_tag & _
        " where rt_serial = " & rt_serial
    
    conSQL.Execute strSQL
  Next

End Sub

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft Excel

From novice to tech pro — start learning today.

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.