[英]Too few parameters in Access VBA
I am trying to run this SQL statement in VBA and for some reason it says there are too few parameters and that it expected 1. I cannot figure out which line its on. 我试图在VBA中运行此SQL语句,由于某种原因,它表示参数太少,并且预期为1。我无法弄清楚它的哪一行。 Any help would be greatly appreciated.
任何帮助将不胜感激。
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 "
I think you have a problem with quotes in your statement text. 我认为您在对帐单文字中的引号有问题。 Look at this excerpt based on your code when I tested in the Immediate window:
在“即时”窗口中进行测试时,请根据您的代码查看以下摘录:
intYear = 2015
? "WHERE [Market Segment]= 'CMS Part D (CY ' & (intYear) & ')'"
WHERE [Market Segment]= 'CMS Part D (CY ' & (intYear) & ')'
That can't be right. 那是不对的。 And when Access tries to execute the query and sees intYear , it interprets that to be a parameter because the db engine knows nothing about a VBA variable named intYear .
当Access试图执行查询并看到intYear时 ,它将解释为参数,因为db引擎对名为inYYear的VBA变量一无所知 。
I think it should look like this: 我认为它应该像这样:
? "WHERE [Market Segment]= 'CMS Part D (CY " & (intYear) & ")'"
WHERE [Market Segment]= 'CMS Part D (CY 2015)'
I encourage you to follow KevenDenen's advice to add Debug.Print strCount2
to the code after it has finished building the strCount2
string. 我鼓励您遵循KevenDenen的建议,在完成构建
strCount2
字符串后,将Debug.Print strCount2
添加到代码中。 Then you can run the code and view the text of the completed statement in the Immediate window. 然后,您可以运行代码并在“即时”窗口中查看已完成语句的文本。 (You can use Ctrl + g to go to the Immediate window.) It helps tremendously to examine the actual statement your code is asking Access to execute.
(您可以使用Ctrl + g转到“立即”窗口。)检查代码要求Access执行的实际语句非常有帮助。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.