运行时错误1004一般odbc错误刷新backgroundquery false

时间:2014-11-27 13:47:43

标签: sql-server excel vba excel-vba

我有一个工作的VBA代码,其运行方式为:

wsEnd.Select

范围( “A:AQ”)。删除

    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之后开始,但在同一个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

结束 结束子

运行代码时,前三个回复正常,但第四个说“运行时错误1004一般odbc错误”

并在backgroundquery = false停止代码。

我已经将SQL代码提升为SQL,并且在那里工作得非常好,甚至尝试在单独的Excel文档上运行它并且不会带来任何乐趣。

复制并粘贴VBA,只更改列表对象表名,即从C到D

我试图将backgroundquery:= false更改为background:= refresh,这有效,但我收到一条消息“运行时错误1004此操作无法完成,因为数据在后台刷新。

请帮忙!

由于 马特

2 个答案:

答案 0 :(得分:1)

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

由于 马特

答案 1 :(得分:0)

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

原始代码:这可以很好地排列字符以读取语句,但通过为每个行项定义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"