在 Access VBA 中计数 SQL 个语句

Count SQL statement in Access VBA

我正在尝试对满足特定条件的访问 table 中的记录进行计数,并将总数插入另一个 table 的一条记录中。我正在通过 vba 这样做,并试图在其中创建一个 sql 语句,但出于某种原因,当我尝试添加时,它说我有太多的行继续,我真的很困惑为什么。任何帮助将不胜感激。

intYear = InputBox("What year is it currently?", "Year Input")

DoCmd.DeleteObject acTable, "ThisTable"



strCreate = "CREATE TABLE MarketSegmentTotals (" & vbCrLf & _
            "[State Medicaid] TEXT," & vbCrLf & _
            "Commercial TEXT," & vbCrLf & _
            "HIX TEXT," & vbCrLf & _
            "MMP TEXT," & vbCrLf & _
            "[CMS Part D (CY " & intYear & ")] TEXT," & vbCrLf & _
            "[CMS Part D (CY " & (intYear + 1) & ")] TEXT" & vbCrLf & _
            ");"


strCount = "INSERT INTO MarketSegmentTotals([State Medicaid], [Commercial], [HIX], [MMP], [CMS Part D (CY " & intYear & ")], [CMS Part D (CY " & (intYear + 1) & ")] ) " & _
"SELECT A.cnt, B.cnt, C.cnt, D.cnt, E.cnt " & _
"FROM ( " & _
    "SELECT COUNT([FORMULARY ID]) as cnt " & _
    "FROM ImportMetricsIDs " & _
    "WHERE [Market Segment]= 'State Medicaid' " & _
") AS A " & _
", ( " & _
    "SELECT COUNT([FORMULARY ID]) as cnt " & _
    "FROM ImportMetricsIDs " & _
    "WHERE [Market Segment]= 'Commercial' " & _
") as B " & _
", ( " & _
    "SELECT COUNT([FORMULARY ID]) as cnt " & _
    "FROM ImportMetricsIDs " & _
    "WHERE [Market Segment]= 'HIX' " & _
") AS C " & _
", ( " & _
    "SELECT COUNT([FORMULARY ID]) as cnt " & _
    "FROM ImportMetricsIDs " & _
    "WHERE [Market Segment]= 'MMP' " & _
") AS D "

VBA 限制由续行符连接的行数。

您可以更改此以重新定义变量以中断行继续数。

strCount = "blahblahblah" & _
  "moreblahblahblah" & _
  "lastblahforabit" 
strCount = strCount & "evenmoreblah" & _
  "toomuchblahblahblah"

或者完全消除续行。

strCount = "blahblahblah"
strCount = strCount & "moreblahblahblah"
strCount = strCount & "lastblahforabit" 
strCount = strCount & "evenmoreblah"
strCount = strCount & "toomuchblahblahblah"