在Access VBA中转换查询

时间:2018-05-21 08:11:54

标签: vba ms-access

我在Access中编写了一个VBA代码。但是,我在TRANSFORM语句中收到语法错误。如果你能帮助我,我将不胜感激。

Sub TransformX1()
    Dim dbs As Database
    Dim strSQL As String
    Set dbs = CurrentDb
    strSQL = "TRANSFORM Sum(BAR1.[TON]) AS SumOfTON" _
             & "SELECT BAR1.[MABD],Sum(BAR1.[TON]) AS [Total Of TON]" _
             & "FROM BAR1" _
             & "WHERE (((BAR1.[MABD]) < 1300) And ((BAR1.[MAGH]) < 1300) And ((BAR1.G) = 1))" _
             & "GROUP BY BAR1.[MABD]" _
             & "PIVOT BAR1.[MAGH]"
    DoCmd.RunSQL strSQL
End Sub

1 个答案:

答案 0 :(得分:1)

如果你在RunSQL命令之前检查你的strSQL变量,你会发现你缺少一些空格。替换

"TRANSFORM Sum(BAR1.[TON]) AS SumOfTON" _

"TRANSFORM Sum(BAR1.[TON]) AS SumOfTON " _

等等。