QueryTables.Add&上的Excel VBA错误SQL Server 2008R2 Express

时间:2012-08-28 16:36:45

标签: sql-server excel-vba oledb vba excel

我正在将Excel报表从访问SQLAnywhere数据库中的数据转换为SQL Server 2008R2 Express数据库,并且在运行时我一直收到错误。用户输入他们想要比较销售动作的一些项目,然后按下按钮以运行宏。宏获取项目,创建SQL查询字符串,获取结果并显示它们。

这与旧数据库完美配合。现在使用SQL Server,如果我运行宏,我会得到一个无用的“400”错误。如果我单步执行代码,在“With ActiveSheets.QueryTables.Add(...)”段的末尾,它会抛出“运行时错误'1004'”。我猜我的SQL语法或OLE DB连接不支持我的语法,但如果我能看到它是什么,那就很麻烦。

我已经完成了生成的SQL查询的Debug.Print,如果我将其复制/粘贴到SQL Server Studio的查询窗口中,它会运行得很好。如果我将查询替换为“select * from DEPT_TAB”之类的简单内容,则可以使用宏运行。创建表也运行正常,但是添加INSERT语句会引发它。

这是宏代码:

Sub SalesHistory()
' Runs SQL from sheet and places into report
' declare method variables
Dim sSQL As String
Dim sConn As String
Dim N1 As Integer
Dim LR As Integer
' create connection string for SQL Server Express
sConn = "OLEDB;Provider=SQLOLEDB;" & _
    "Data Source=192.168.0.29\SQLEXPRESS;" & _
    "Initial Catalog=STORESQL;" & _
    "User ID=xxxxx;Password=xxxxx;Trusted_Connection=False;"

' stop screen updating while running
Application.ScreenUpdating = False

' select SQL sheet and get number of rows in column A
Sheets("SQL").Select
LR = Range("A" & Rows.Count).End(xlUp).Row

' loop through column A and create SQL command string
For N1 = 1 To LR
sSQL = sSQL & " " & Range("A" & N1).Value
Next N1

' test print sSQL
Debug.Print sSQL

' select Main sheet, clear report, and activate for new results
Sheets("Main").Select
Range("$A$2:$D$51").ClearContents
Sheets("Main").Activate

' use QueryTables.Add() to get data from server
With ActiveSheet.QueryTables.Add(Connection:=sConn, Destination:=Range("A2))
    .CommandText = sSQL
    .Name = "Movement"
    .FieldNames = False
    .RowNumbers = False
    .FillAdjacentFormulas = False
    .PreserveFormatting = True
    .RefreshOnFileOpen = False
    .BackgroundQuery = True
    .RefreshStyle = xlOverwriteCells
    .SavePassword = False
    .SaveData = True
    .AdjustColumnWidth = True
    .RefreshPeriod = 0
    .PreserveColumnInfo = True
    .Refresh BackgroundQuery:=False
End With  
' HERE IS WHERE THE ERROR IS THROWN

' restart ScreenUpdating
Application.ScreenUpdating = True

End Sub

以下是工作表中的查询:

-- declare variables
DECLARE @StartDate DATE;
DECLARE @Location CHAR(3);
SET @StartDate = '07/29/2012';
SET @Location = 'PAL';
-- create temp table for holding results
CREATE TABLE #tmp_ItemMove (
UPC CHAR(13) NULL,
Brand CHAR(32) NULL,
Descriptor CHAR(32) NULL,
Sales INT NULL);
-- insert zero values for each item so that they always show up in final report
INSERT INTO #tmp_ItemMove 
SELECT OBJ.F01, OBJ.F155, OBJ.F29, 0
FROM OBJ_TAB OBJ
WHERE OBJ.F01 LIKE '' OR
OBJ.F01 LIKE '' OR
OBJ.F01 LIKE '' OR
OBJ.F01 LIKE '' OR
OBJ.F01 LIKE '';
-- insert sales information into temp table
INSERT INTO #tmp_ItemMove
SELECT OBJ.F01, OBJ.F155, OBJ.F29, 
CASE WHEN TRS.F67=0 THEN TRS.F64 ELSE TRS.F67 END
FROM
RPT_ITM_D TRS JOIN OBJ_TAB OBJ ON TRS.F01=OBJ.F01
JOIN TLZ_TAB TLZ ON TRS.F1034=TLZ.F1034
JOIN LNK_TAB LNK ON TRS.F1056=LNK.F1056 AND TRS.F1057=LNK.F1057 AND LNK.F1000=@Location
WHERE (OBJ.F01 LIKE '' OR
OBJ.F01 LIKE '' OR
OBJ.F01 LIKE '' OR
OBJ.F01 LIKE '' OR
OBJ.F01 LIKE '') AND
TRS.F254 >= @StartDate AND
TRS.F1034 BETWEEN 3 AND 4;
-- get final report
-- sum sales history for each item and display
SELECT MAX(UPC),
MAX(Brand),
MAX(Descriptor),
SUM(Sales)
FROM #tmp_ItemMove
GROUP BY UPC, Brand, Descriptor
ORDER BY SUM(Sales) DESC, Brand, UPC;
DROP TABLE #tmp_ItemMove;

感谢任何帮助,谢谢。

1 个答案:

答案 0 :(得分:0)

您是否尝试在应用中运行查询时运行跟踪?运行将显示所有错误和异常的跟踪。这应该向您显示确切的失败点。如果有许多交易正在进行,您将需要应用一些过滤器。这可能会有所帮助:http://www.bbdevnetwork.com/blogs/debugging-t-sql-and-sql-profiler/但请确保您启用了错误。

表格是否具有适当的权限?如果您将其粘贴到SSMS窗口中,则可能在该窗口中以dbo或sa运行,这将允许它在excel中的用户可能具有受限权限时运行。

您应该将该查询放入存储过程并执行该操作,因为运行此类查询可能会出现多个故障点,并且许多驱动程序不会向您提供任何有用的错误信息。