【发布时间】: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