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

我有一个工作的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 

最后,

我有两个其他的脚本开始之后,但在同一个子使用相同的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:=假的背景:=刷新,这个工程,但我得到一个消息,说:“运行时错误1004这个操作不能完成,因为数据刷新在后台。

请帮忙!

谢谢Matt

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

谢谢Matt

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

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

修改:除了颜色格式在翻译中迷路了。 这告诉任何人读它的variables是设置一次,并消除多余的字符必须扫描。

 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"