【问题标题】:run time error 1004 general odbc error refresh backgroundquery false运行时错误 1004 一般 odbc 错误刷新 backgroundquery false
【发布时间】:2015-01-26 03:23:58
【问题描述】:

我有一个运行的 VBA 代码:

wsEnd.Select

Range("A:AQ").Delete

        strSQL = "Select *
        strSQL = strSQL & " FROM [XXX].[ABCCustomer] As A"
        strSQL = strSQL & " Left join"
        strSQL = strSQL & " (Select * "
        strSQL = strSQL & " From [XXX]..[ABCCustomer]"
        strSQL = strSQL & " where LineageId = '123' ) B"
        strSQL = strSQL & " on a.product = b.product and a.[StartDate] = b.[StartDate]"
        strSQL = strSQL & " where (a.EndDate <> b.EndDate)"
        strSQL = strSQL & " and a.NewEndDate is NULL AND B.NewEndDate IS NULL"
        strSQL = strSQL & " and a.Id = '456"
        strSQL = strSQL & " order by b.ProductType"

    With ActiveSheet.ListObjects.Add(SourceType:=0, Source:=Array(Array( _
        "ODBC;DRIVER=SQL Server;SERVER=XXX\SQL01;UID=;Trusted_Connection=Yes;APP=2007 Microsoft  Office system;WSID=XXX;DATA" _
        ), Array("BASE=master")), Destination:=Range("$A$1")).QueryTable
        .CommandText = strSQL
        .RowNumbers = False
        .FillAdjacentFormulas = False
        .PreserveFormatting = True
        .RefreshOnFileOpen = False
        .BackgroundQuery = True
        .RefreshStyle = xlInsertDeleteCells
        .SavePassword = False
        .SaveData = True
        .AdjustColumnWidth = True
        .RefreshPeriod = 0
        .PreserveColumnInfo = True
        .ListObject.DisplayName = "Table_Query_from_XXX_C"
        .Refresh BackgroundQuery:=False

End with

我还有另外两个脚本在 End With 之后开始,但在同一个 sub 中都使用相同的 VBA 只是不同的 SQL,它们都工作得很好。

然后我有这个非常烦人,这让我非常头疼,如下:

        strSQL = "Select *
        strSQL = strSQL & " FROM [XXX].[ABCCustomer] As A"
        strSQL = strSQL & " Left join"
        strSQL = strSQL & " (Select * "
        strSQL = strSQL & " From [XXX]..[ABCCustomer]"
        strSQL = strSQL & " where Id = '123' ) B"
        strSQL = strSQL & " on a.product = b.product and a.[StartDate] = b.[StartDate]"
        strSQL = strSQL & " where (a.EndDate = b.EndDate)"
        strSQL = strSQL & " and a.NewEndDate is Not NULL AND B.NewEndDate not NULL"
        strSQL = strSQL & " and a.Id = '456"
        strSQL = strSQL & " order by b.Product"

        With ActiveSheet.ListObjects.Add(SourceType:=0, Source:=Array(Array( _
        "ODBC;DRIVER=SQL Server;SERVER=XXX\SQL01;UID=;Trusted_Connection=Yes;APP=2007 Microsoft Office system;WSID=XXX;DATA" _
        ), Array("BASE=master")), Destination:=Range("$A$1")).QueryTable
        .CommandText = strSQL
        .RowNumbers = False
        .FillAdjacentFormulas = False
        .PreserveFormatting = True
        .RefreshOnFileOpen = False
        .BackgroundQuery = True
        .RefreshStyle = xlInsertDeleteCells
        .SavePassword = False
        .SaveData = True
        .AdjustColumnWidth = True
        .RefreshPeriod = 0
        .PreserveColumnInfo = True
        .ListObject.DisplayName = "Table_Query_from_XXX_D"
        .Refresh BackgroundQuery:=False
    End With
End Sub

运行代码时,前三个恢复正常,但第四个说

运行时错误 1004 一般 odbc 错误

并在backgroundquery=false处停止代码。

我已将 SQL 代码提升到 SQL 中,它在那里工作得非常好,甚至尝试在单独的 excel 文档上运行它,但这并没有带来任何乐趣。

复制和粘贴 VBA,仅更改列表对象表名,即从 C 更改为 D

我尝试将backgroundquery:=false 更改为background:=refresh,这可行,但我收到一条消息说

运行时错误 1004 由于后台正在刷新数据,因此无法执行此操作。

【问题讨论】:

  • 为什么要每行重置变量值?
  • 嗨,您指的是每个 strSQL = strSQL & 吗?如果是这样,无非是为了让同事等容易阅读。除此之外没有其他原因。
  • 请注意:您在“选择 * 后缺少右引号,这会导致其余代码格式错误。
  • 嗨拉德克。你是绝对正确的,很好看☺。但是,随着 select 语句持续一段时间,我已将提供的示例的各个部分更改为更通用的内容。请相信我的话,未更改的代码末尾有“。我已经将 sql 位复制并粘贴到 sql 中,它工作得非常好。我觉得错误不是 sql,而是其他东西。任何进一步的想法都超过欢迎

标签: sql-server vba excel


【解决方案1】:

计算机重置后错误消失。真的很抱歉,但感谢所有回复的人。

谢谢 马特

【讨论】:

    【解决方案2】:

    这不是问题的答案。但它是相关的,因为这样写的原因是为了让它更容易阅读。

    原始代码:这可以很好地排列字符以读取语句,但是通过为每个行项定义 strSQL 的值是多余的。

    strSQL = "Select *
    strSQL = strSQL & " FROM [XXX].[ABCCustomer] As A"
    strSQL = strSQL & " Left join"
    strSQL = strSQL & " (Select * "
    strSQL = strSQL & " From [XXX]..[ABCCustomer]"
    strSQL = strSQL & " where LineageId = '123' ) B"
    strSQL = strSQL & " on a.product = b.product and a.[StartDate] = b.[StartDate]"
    strSQL = strSQL & " where (a.EndDate <> b.EndDate)"
    strSQL = strSQL & " and a.NewEndDate is NULL AND B.NewEndDate IS NULL"
    strSQL = strSQL & " and a.Id = '456"
    strSQL = strSQL & " order by b.ProductType"
    

    修改:除了颜色格式在翻译中丢失。这告诉任何阅读它的人该变量被设置一次,并消除了必须扫描的冗余字符。

    strSQL = "Select * " & _
             "FROM [XXX].[ABCCustomer] As A " & _
             "Left join " & _
             "(Select * " & _
             "From [XXX]..[ABCCustomer] " & _
             "where LineageId = '123' ) B " & _
             "on a.product = b.product and a.[StartDate] = b.[StartDate] " & _
             "where (a.EndDate <> b.EndDate) " & _
             "and a.NewEndDate is NULL AND B.NewEndDate IS NULL " & _
             "and a.Id = '456 " & _
             "order by b.ProductType"
    

    【讨论】:

    • 总是乐于获得新的信息,非常感谢。但是,是的,问题仍然存在。对于阅读的任何人,我都被告知要尝试清除脚本中的缓存。这对任何人都有意义吗?我该如何进行。如果有任何区别,则连接到 SQL Server Management Studio。谢谢
    • 希望我能在 SQL 连接上为您提供更多帮助。我最近才开始使用它,还没有在 Excel 中使用它。关于上面的代码:我觉得要求不高,不是说几个字符很难处理,而是每次向变量添加更多文本时不必读取之前的值。随着每一行的增加,它的要求呈指数级增长。几行字符串再次没什么大不了的,但理论上......它设置变量一次而不是设置它 11x 并读取它 10x,每次都增加长度。我=这里的书呆子..
    猜你喜欢
    • 2013-11-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-04-15
    • 2016-04-03
    • 2018-07-07
    相关资源
    最近更新 更多