结合SQL插入INTO

时间:2014-12-29 19:06:36

标签: sql vba insert ms-access-2010

我正在尝试在VBA中组合3个SQL状态网,以便它们在表中显示为一条记录。由于某种原因,VBA引发的参数错误太少。这是我的调试打印语句输出的内容:

INSERT INTO Totals
            ([TOTAL VERIFIED FORMULARIES],[TOTAL AVAILABLE FOR IMPORT],[TOTAL       SHOULD BE IMPORTED])
SELECT A.cnt,B.cnt,C.cnt
FROM   (SELECT Count([FORMULARY ID]) AS cnt
        FROM   VerifiedFormularies) AS A,
       (SELECT Count([FORMULARY ID]) AS cnt
        FROM   ImportMetricsIDs) AS B,
       (SELECT Count([FORMULARY ID]) AS cnt
        FROM   ShouldImportMetricsIDsTable
        WHERE  [IMPORT STATUS] = 'Yes') AS C 

这是我的代码:

totalVerified = "INSERT INTO Totals([TOTAL VERIFIED FORMULARIES], [TOTAL AVAILABLE FOR IMPORT], [TOTAL SHOULD BE IMPORTED]) " & _
"SELECT A.cnt, B.cnt, C.cnt " & _
"FROM ( " & _
    "SELECT COUNT([FORMULARY ID]) as cnt " & _
    "FROM VerifiedFormularies " & _
") AS A " & _
", ( " & _
    "SELECT COUNT([FORMULARY ID]) as cnt " & _
    "FROM ImportMetricsIDs " & _
") as B " & _
", ( " & _
    "SELECT COUNT([FORMULARY ID]) as cnt " & _
    "FROM ShouldImportMetricsIDsTable " & _
    "WHERE [IMPORT STATUS]= 'Yes' " & _
") AS C "

我试过调试但是没有成功。任何帮助将不胜感激!

1 个答案:

答案 0 :(得分:0)

我想我明白了。你错过了上一次

totalVerified = "INSERT INTO Totals([TOTAL VERIFIED FORMULARIES], [TOTAL AVAILABLE FOR IMPORT], [TOTAL SHOULD BE IMPORTED]) " & vbcr & _
"SELECT [TOTAL VERIFIED FORMULARIES], [TOTAL AVAILABLE FOR IMPORT], [TOTAL SHOULD BE IMPORTED] " & vbcr & _
"FROM ( " & vbcr & _
    "SELECT COUNT([FORMULARY ID]) as cnt " & vbcr & _
    "FROM VerifiedFormularies " & vbcr & _
") AS [TOTAL VERIFIED FORMULARIES], " & vbcr & _
"( " & vbcr & _
    "SELECT COUNT([FORMULARY ID]) as cnt " & vbcr & _
    "FROM ImportMetricsIDs " & vbcr & _
") AS [TOTAL AVAILABLE FOR IMPORT], " & vbcr & _
"( " & vbcr & _
    "SELECT COUNT([FORMULARY ID]) as cnt " & vbcr & _
    "FROM ShouldImportMetricsIDsTable " & vbcr & _
    "WHERE [IMPORT STATUS]= 'Yes' " & vbcr & _
") AS [TOTAL SHOULD BE IMPORTED] " & vbcr & _
")" 

我建议在查询中使用相同的名称。请注意,该数据类型必须相同!