Excel Query to update a SQL db

Posted on 2011-10-20
Last Modified: 2012-08-14
I am using Excel to update a SQL DB table (I do not have the luxury of moving to a different tool to accomplish this for the user).  The code works fine if I only make one update.  However, I need to step through a number of updates and that's when I get an error:
"Runtime Error 1004: the operation cannot be done because the data is refreshing in the background."

If I step through the code in debug mode, I don't get any errors and all items get updated.
If I change .BackgroundQuery = True to .BackgroundQuery = False I get another error, essentially telling me the same thing.

Is there anything I can do in my code to allow for multiple updates without this error?

Sub UpdateSampleTable()
    Dim strText As String
    Dim iRow As Integer
    For iRow = 1 To 10
        strText = "update computers set testvar = " & iRow
        With ActiveWorkbook.Connections("SampleQuery").ODBCConnection
            .BackgroundQuery = True
            .CommandText = Array(strText)
            .CommandType = xlCmdSql
            .Connection = "ODBC;DRIVER=SQL Server;SERVER=sql;UID=username;Trusted_Connection=Yes;APP=Microsoft Office 2010;WSID=USERNAME"
            .RefreshOnFileOpen = False
            .SavePassword = False
            .SourceConnectionFile = ""
            .SourceDataFile = ""
            .ServerCredentialsMethod = xlCredentialsMethodIntegrated
            .AlwaysUseConnectionFile = False
        End With
End Sub
Question by:malcolm29
    1 Comment
    LVL 85

    Accepted Solution

    That's an unusual way of doing things. I would use ADO:

       Dim conn1 As Object
        ' Create the Connection object.
       Set conn1 = CreateObject("ADODB.Connection")
       With conn1
          .Provider = "sqloledb"
          .ConnectionString = "DRIVER={SQL Server};Server=SERVER=sql;UID=username;Trusted_Connection=Yes;APP=Microsoft Office 2010;WSID=USERNAME"
       End With
    For iRow = 1 to 10
       Conn1.Execute "update computers set testvar = " & iRow
    next iRow

    Open in new window


    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    How your wiki can always stay up-to-date

    Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
    - Increase transparency
    - Onboard new hires faster
    - Access from mobile/offline

    Having an SQL database can be a big investment for a small company. Hardware, setup and of course, the price of software all add up to a big bill that some companies may not be able to absorb.  Luckily, there is a free version SQL Express, but does …
    Let's review the features of new SQL Server 2012 (Denali CTP3). It listed as below: PERCENT_RANK(): PERCENT_RANK() function will returns the percentage value of rank of the values among its group. PERCENT_RANK() function value always in be…
    Viewers will learn how to use the SELECT statement in SQL to return specific rows and columns, with various degrees of sorting and limits in place.
    This Micro Tutorial will demonstrate how to create pivot charts out of a data set. I also added a drop-down menu which allows to choose from different categories in the data set and the chart will automatically update.

    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

    8 Experts available now in Live!

    Get 1:1 Help Now