在通过VBA访问时运行多个普通和交叉表查询的组合

时间:2015-10-29 21:22:09

标签: sql ms-access access-vba crosstab

我想在Access中一起运行多个交叉表和普通查询。为此,我从#Albert D. Kallal here

提供的答案中获得了帮助

想法是将多个查询存储在由分隔符分隔的文本文件中并解析并通过VBA执行它们。使用这种技术,我能够成功运行多个普通查询。但是,当我尝试运行包含交叉表查询时,我会在弹出消息框时收到以下错误。

运行时错误'3000': 保留错误(-3002);此错误没有消息。

以下是我的代码和数据的可重现版本。

Sub SqlScripts()

   Dim vSql       As Variant
   Dim vSqls      As Variant
   Dim vList      As Variant
   Dim vLists     As Variant
   Dim strSql     As String
   Dim strList    As String
   Dim intF       As Integer
   Dim intL       As Integer
   Dim TableName  As String
   Dim QueryFile  As String
   Dim TableList  As String

   QueryFile = "C:\Staffing_queries.sql"
   TableList = "C:\staffing_tables.txt"        

   intF = FreeFile()
   Open QueryFile For Input As #intF

   intL = FreeFile()
   Open TableList For Input As #intL

   strSql = Input(LOF(intF), #intF)
   strList = Input(LOF(intL), #intL)
   Close intF
   Close intL

   vSql = Split(strSql, ";")
   vList = Split(strList, ";")

     For Each vLists In vList
        TableName = "" & vLists

        If TableExists(TableName) Then
            CurrentDb.TableDefs.Delete TableName
            Debug.Print TableName & " Deleted"
        End If
    Next

'   On Error Resume Next

   For Each vSqls In vSql
        If vSqls <> "" Then
            Debug.Print vSqls
            CurrentDb.Execute vSqls
        End If
   Next

End Sub

Function TableExists(TableName As String) As Boolean
    On Error Resume Next
    Dim strName As String
    strName = CurrentDb.TableDefs(TableName).Name
    TableExists = (Err.Number = 0)
End Function

Staffing_queries.sql的内容(一个交叉表和一个普通查询)

TRANSFORM Sum(Compens_Data.GRS_YTD) AS SumOfGRS_YTD
SELECT Compens_Data.EMPLID
INTO Test_spot_bonus
FROM Compens_Data
WHERE ((Compens_Data.EARNINGS_DESCRIPTION)="Spot Bonus") 
GROUP BY Compens_Data.EMPLID
PIVOT Compens_Data.BALANCE_YEAR;

SELECT EMPLID, BALANCE_YEAR, 
Sum(IIF(EARNINGS_DESCRIPTION="Spot Bonus",GRS_YTD,0)) as SPOT_BONUS,
Sum(IIF(EARNINGS_DESCRIPTION="Incentive Compensation",GRS_YTD,0)) as Incentive_Comp,
Sum(IIF(EARNINGS_DESCRIPTION="STIC Target",GRS_YTD,0)) as STIC_TRGT
INTO Benefits_Data_Temp
FROM Compens_Data
GROUP BY EMPLID, BALANCE_YEAR
;

Staffing_table.txt的内容(由VBA代码用于删除现有表格)

Test_spot_bonus;Benefits_Data_Temp;

数据样本(找不到附加非图像文件的任何选项)

EMPLID  BALANCE_YEAR    EARNINGS_DESCRIPTION    GRS_YTD
A1  2013    Incentive Compensation  4,926.67
A1  2013    STIC Target 5,108.00
A1  2014    Incentive Compensation  6,230.00
A1  2014    STIC Target 5,394.67
A1  2014    Spot Bonus  720
A1  2015    Incentive Compensation  6,946.67
A2  2013    Incentive Compensation  7,663.33
A2  2013    STIC Target 9,652.67
A2  2014    Incentive Compensation  12,652.00
A2  2014    STIC Target 9,813.33
A2  2015    Incentive Compensation  12,083.33
A3  2013    Incentive Compensation  8,505.33
A3  2013    STIC Target 11,465.33
A3  2014    Incentive Compensation  13,240.00
A3  2014    STIC Target 11,415.33
A3  2015    Incentive Compensation  12,790.00

1 个答案:

答案 0 :(得分:2)

该错误与VBA代码无关,您无法直接将交叉表查询与表创建查询相结合。

TRANSFORM ... SELECT ... INTO ... FROM ... 
从Access查询窗口运行时,

给出相同的Reserved error(-3002)

您需要创建一个querydef,将其.SQL设置为TRANSFORM查询,然后使用此查询作为SELECT INTO的基础:

SELECT * INTO Test_spot_bonus FROM MyCrosstabQuery