计入访问 VBA
Count in Access VBA
我试图通过在 VBA 中使用以下代码将计数统计信息作为一条记录插入到 table 中。但出于某种原因,当我去执行它时,它说查询值的数量和目标字段不一样,我很困惑为什么会这样。任何帮助将不胜感激。
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, F.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 "
strCount2 = strCount & _
", ( " & _
"SELECT COUNT([FORMULARY ID]) as cnt " & _
"FROM ImportMetricsIDs " & _
"WHERE [Market Segment]= 'CMS Part D (CY ' & (intYear) & ')' " & _
") AS E " & _
", ( " & _
"SELECT COUNT([FORMULARY ID]) as cnt " & _
"FROM ImportMetricsIDs " & _
"WHERE [Market Segment]= 'CMS Part D (CY ' & (intYear + 1) & ')' " & _
") AS F "
db.Execute strCreate
db.Execute strCount2
您的查询中似乎缺少 space:
"SELECT A.cnt, B.cnt, C.cnt, D.cnt, E.cnt, F.cnt" & _
"FROM ( " & _
应该是
"SELECT A.cnt, B.cnt, C.cnt, D.cnt, E.cnt, F.cnt " & _
"FROM ( " & _
或
"SELECT A.cnt, B.cnt, C.cnt, D.cnt, E.cnt, F.cnt" & _
" FROM ( " & _
我更喜欢第一行末尾带有 space 的第一个,因为它保持了 select 和 from.
的排列
我试图通过在 VBA 中使用以下代码将计数统计信息作为一条记录插入到 table 中。但出于某种原因,当我去执行它时,它说查询值的数量和目标字段不一样,我很困惑为什么会这样。任何帮助将不胜感激。
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, F.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 "
strCount2 = strCount & _
", ( " & _
"SELECT COUNT([FORMULARY ID]) as cnt " & _
"FROM ImportMetricsIDs " & _
"WHERE [Market Segment]= 'CMS Part D (CY ' & (intYear) & ')' " & _
") AS E " & _
", ( " & _
"SELECT COUNT([FORMULARY ID]) as cnt " & _
"FROM ImportMetricsIDs " & _
"WHERE [Market Segment]= 'CMS Part D (CY ' & (intYear + 1) & ')' " & _
") AS F "
db.Execute strCreate
db.Execute strCount2
您的查询中似乎缺少 space:
"SELECT A.cnt, B.cnt, C.cnt, D.cnt, E.cnt, F.cnt" & _
"FROM ( " & _
应该是
"SELECT A.cnt, B.cnt, C.cnt, D.cnt, E.cnt, F.cnt " & _
"FROM ( " & _
或
"SELECT A.cnt, B.cnt, C.cnt, D.cnt, E.cnt, F.cnt" & _
" FROM ( " & _
我更喜欢第一行末尾带有 space 的第一个,因为它保持了 select 和 from.
的排列