Too few parameters in Access VBA - sql

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. 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.
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. 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.

Related

add a row to query in MS-ACCESS SQL

I'm trying to add to the following query:
strSQL = "SELECT fldName, blkName, CDbl(fldValue) " & _
"FROM dbSecurities2 as S " & _
"WHERE " & _
"S.isin='" & Code & "' " & _
"AND " & _
"S.fldName='" & fldName & "' "
A row that makes the sum of the fldValue like:
strSQL = "SELECT fldName, blkName, CDbl(fldValue) " & _
"FROM dbSecurities2 as S " & _
"UNION " & _
"SELECT Sum(fldValue) AS fldValue " & _
"WHERE " & _
"S.isin='" & Code & "' " & _
"AND " & _
"S.fldName='" & fldName & "' "
the error is:
Run -time error '3141'. The SELECT statement includes a reserved word or an argument name that is misspelled or missing, or the punctuation is incorrect
I found this is working:
strSQL = "SELECT fldName, blkName, CDbl(fldValue) " & _
"FROM dbSecurities2 as S " & _
"WHERE " & _
"S.isin='" & Code & "' " & _
"AND " & _
"S.fldName='" & fldName & "' " & _
"UNION " & _
"SELECT '' AS fldName, 'Total' AS Total, Sum(CDbl(fldValue)) " & _
"FROM dbSecurities2 AS B " & _
"WHERE " & _
"B.isin='" & Code & "' " & _
"AND " & _
"B.fldName='" & fldName & "' "
This should run as expected:
strSQL = "SELECT fldName, blkName, CDbl(fldValue) " & _
"FROM dbSecurities2 AS S " & _
"WHERE " & _
"S.isin='" & Code & "' " & _
"AND " & _
"S.fldName='" & fldName & "' " & _
"UNION ALL " & _
"SELECT TOP 1 "", "Total", Sum(CDbl(fldValue)) " & _
"FROM dbSecurities2"
If you have Null values, use Nz:
strSQL = "SELECT fldName, blkName, CDbl(Nz(fldValue, 0)) " & _
"FROM dbSecurities2 AS S " & _
"WHERE " & _
"S.isin='" & Code & "' " & _
"AND " & _
"S.fldName='" & fldName & "' " & _
"UNION ALL " & _
"SELECT TOP 1 "", "Total", Sum(CDbl(Nz(fldValue, 0))) " & _
"FROM dbSecurities2"

VB for MS Access not working when using INNER JOIN

The code isn't working properly, getting a Syntax error in INSERT INTO statement error. Trying to insert data with an inner join from an MS Access form where the user types in responses and selects a button to add the detail to a new table. Based on the information provided I want it to insert the data into another table where it is joined by a table to get the fiscal data.
stSQL = "INSERT INTO PO_Information " & _
"(Partner, PO_Number, PO_Title, Cost_Center, Description, Line_1_Amt, Line_2_Amt, Date_Added, Month_Added, Year_Added, FY, Qtr, FY_Qtr, FW, FWeek)" & _
"Select New_PO_Information.Partner, " & _
"New_PO_Information.PO_Number, " & _
"New_PO_Information.PO_Title, " & _
"New_PO_Information.Cost_Center, " & _
"New_PO_Information.Description, " & _
"New_PO_Information.Line_1_Amt, " & _
"New_PO_Information.Line_2_Amt, " & _
"New_PO_Information.Date_Added, " & _
"New_PO_Information.Month_Added, " & _
"New_PO_Information.Year_Added, " & _
"Fiscal_Calendar.FY, " & _
"Fiscal_Calendar.Qtr, " & _
"Fiscal_Calendar.FY_Qtr, " & _
"Fiscal_Calendar.FW, " & _
"Fiscal_Calendar.FWeek, " & _
"From New_PO_Information INNER JOIN Fiscal_Calendar " & _
"ON New_PO_Information.Date_Added = Fiscal_Calendar.Calendar"
stSQL = stSQL & _
"From New_PO_Information " & _
"Where ((New_PO_Information.Partner)<>'') " & _
"AND ((New_PO_Information.PO_Number)<>'') " & _
"AND ((New_PO_Information.PO_Title)<>'') " & _
"AND ((New_PO_Information.Cost_Center)<>'') " & _
"AND ((New_PO_Information.Description)<>'');"
Can someone tell me where I went wrong and correct the code, please?
It's probably this comma here: "Fiscal_Calendar.FWeek, " & _
There shouldn't be a comma before the FROM statement. And make sure you only have one FROM statement:
stSQL = "INSERT INTO PO_Information " & _
"(Partner, PO_Number, PO_Title, Cost_Center, Description, Line_1_Amt, Line_2_Amt, Date_Added, Month_Added, Year_Added, FY, Qtr, FY_Qtr, FW, FWeek)" & _
"Select New_PO_Information.Partner, " & _
"New_PO_Information.PO_Number, " & _
"New_PO_Information.PO_Title, " & _
"New_PO_Information.Cost_Center, " & _
"New_PO_Information.Description, " & _
"New_PO_Information.Line_1_Amt, " & _
"New_PO_Information.Line_2_Amt, " & _
"New_PO_Information.Date_Added, " & _
"New_PO_Information.Month_Added, " & _
"New_PO_Information.Year_Added, " & _
"Fiscal_Calendar.FY, " & _
"Fiscal_Calendar.Qtr, " & _
"Fiscal_Calendar.FY_Qtr, " & _
"Fiscal_Calendar.FW, " & _
"Fiscal_Calendar.FWeek " & _
"FROM New_PO_Information INNER JOIN Fiscal_Calendar " & _
"ON New_PO_Information.Date_Added = Fiscal_Calendar.Calendar"
stSQL = stSQL & _
" Where ((New_PO_Information.Partner)<>'') " & _
"AND ((New_PO_Information.PO_Number)<>'') " & _
"AND ((New_PO_Information.PO_Title)<>'') " & _
"AND ((New_PO_Information.Cost_Center)<>'') " & _
"AND ((New_PO_Information.Description)<>'');"
If I created an append query, here is the code. Just trying to convert this into my SQL statement that I posted earlier.
INSERT INTO PO_Information ( Partner, PO_Number, PO_TItle, Cost_Center, Description, Line_1_Amt, Line_2_Amt, Date_Added, Month_Added, Year_Added, FY, Qtr, FY_Qtr, FW, FWeek )
SELECT New_PO_Information.Partner, New_PO_Information.PO_Number, New_PO_Information.PO_TItle, New_PO_Information.Cost_Center, New_PO_Information.Description, New_PO_Information.Line_1_Amt, New_PO_Information.Line_2_Amt, New_PO_Information.Date_Added, New_PO_Information.Month_Added, New_PO_Information.Year_Added, Fiscal_Calendar.FY, Fiscal_Calendar.Qtr, Fiscal_Calendar.FY_Qtr, Fiscal_Calendar.FW, Fiscal_Calendar.FWeek
FROM New_PO_Information INNER JOIN Fiscal_Calendar ON New_PO_Information.Date_Added = Fiscal_Calendar.Calendar;

use variables in sql query in access

How to modify below sql query in Access VBA, where 6021, 1, and EachYear are variables.
SELECT & Edgar_Gen.[SIC Code], Edgar_Fin.EntityID, Edgar_Fin.IsANN, Edgar_Fin.[Fiscal Year],
Formula AS val
FROM Edgar_Fin INNER JOIN Edgar_Gen ON Edgar_Fin.EntityID = Edgar_Gen.EntityID
WHERE (((Edgar_Gen.[SIC Code]) = 6021) AND
((Edgar_Fin.IsANN)="1") AND ((Edgar_Fin.[Fiscal Year])="EachYear"));
It's not 100% clear what you are referring to when you're asking to "modify below sql query" for 6021 1 and EachYear, but this is the basic idea and structure of what you seem to be trying to accomplish:
Variable1 = 6021
Variable2 = 1
Variable3 = 2018
CurrentDb.Execute "SELECT Edgar_Gen.[SIC Code], " & _
"Edgar_Fin.EntityID, " & _
"Edgar_Fin.IsANN, " & _
"Edgar_Fin.[Fiscal Year], " & _
"Formula AS val " & _
"FROM Edgar_Fin " & _
"INNER JOIN Edgar_Gen ON Edgar_Fin.EntityID = Edgar_Gen.EntityID " & _
"WHERE ( " & _
"((Edgar_Gen.[SIC Code]) = " & Variable1 & ") " & _
"AND ((Edgar_Fin.IsANN) = " & Variable2 & ") " & _
"AND ((Edgar_Fin.[Fiscal Year]) = " & Variable3 & ") " & _
") "

Combine SQL Insert INTO

I am trying to combine 3 SQL statemnets in VBA so that they show up as one record in a table. For some reason VBA throws a too few parameters error. Here is what my debug print statement outputs:
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
And here is my code:
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 "
I have tried to debug it but was unsuccessful. Any help would be greatly appreciated!
I think i got it. You missed last )
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 & _
")"
I'd suggest to use the same names in query. Note, that data type must be the same!

Count SQL statement in Access VBA

I am trying to count records in an access table where certain criteria are met and insert the totals into another table in one record. I am doing so through vba and am trying to create a sql statement in there but for some reason it says I have too many line continuations when I try to add in anymore and I am really confused as to why. Any help would be greatly appreciated.
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 limits the number of lines joined by line continuation characters.
You can change this to redefine the variable to break the number of line continuation.
strCount = "blahblahblah" & _
"moreblahblahblah" & _
"lastblahforabit"
strCount = strCount & "evenmoreblah" & _
"toomuchblahblahblah"
Or eliminate the line continuation entirely.
strCount = "blahblahblah"
strCount = strCount & "moreblahblahblah"
strCount = strCount & "lastblahforabit"
strCount = strCount & "evenmoreblah"
strCount = strCount & "toomuchblahblahblah"