Combine SQL Insert INTO - sql

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!

Related

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;

SQL where with Union and Left

Is it possible to add criteria in [ID] and not in [TypeID] in Left Join?
SQL = "SELECT " & _
"ADate As NewDate, " & _
"tblA.TypeID as ID, " & _
"tblAB.TypeControl as ControlID " & _
"FROM tblA " & _
"LEFT OUTER JOIN tblAB " & _
"ON tblAB.TypeID = tblA.TypeID " & _
"WHERE tblA.TypeID = " & Counter & " " & _ => Delete this one.
"UNION ALL SELECT " & _
"BDate As NewDate, " & _
"tblB.TypeID as ID, " & _
"tblAB.TypeControl as ControlID " & _
"FROM tblB " & _
"LEFT OUTER JOIN tblAB " & _
"ON tblAB.TypeID = tblB.TypeID " & _
"WHERE tblB.TypeID = " & Counter & " " & _ => Delete this one.
===
and place one WHERE on ID here
"WHERE ID = " & Counter & " " & _ => Like this one. But I am getting an error.
===
"ORDER BY NewDate;"
Delete the two WHERE from tblA and tblB.
Add one in ID in the end.
And create this one.
SQL = "SELECT " & _
"ADate As NewDate, " & _
"tblA.TypeID as ID, " & _
"tblAB.TypeControl as ControlID " & _
"FROM tblA " & _
"LEFT OUTER JOIN tblAB " & _
"ON tblAB.TypeID = tblA.TypeID " & _
"UNION ALL SELECT " & _
"BDate As NewDate, " & _
"tblB.TypeID as ID, " & _
"tblAB.TypeControl as ControlID " & _
"FROM tblB " & _
"LEFT OUTER JOIN tblAB " & _
"ON tblAB.TypeID = tblB.TypeID " & _
"WHERE tblB.TypeID = " & Counter & " " & _
"WHERE ID = " & Counter & " " & _
"ORDER BY NewDate;"
Thank you in advance.
You can check for ID's only once by wrapping your entire query in a subquery, and then check for IDs in the outer query, e.g.:
SELECT * FROM (
SELECT " & _
"ADate As NewDate, " & _
"tblA.TypeID as ID, " & _
"tblAB.TypeControl as ControlID " & _
"FROM tblA " & _
"LEFT OUTER JOIN tblAB " & _
"ON tblAB.TypeID = tblA.TypeID " & _
"UNION ALL SELECT " & _
"BDate As NewDate, " & _
"tblB.TypeID as ID, " & _
"tblAB.TypeControl as ControlID " & _
"FROM tblB " & _
"LEFT OUTER JOIN tblAB " & _
"ON tblAB.TypeID = tblB.TypeID "
) WHERE ID = " & Counter & "
However, depending on how the database engine optimizes this, it might take longer to execute. I recommend you don't do this, and leave your query as-is.
(Also, I leave the quotes mess to you, since your question shouldn't have these anyway).

SQL Report Query To DataGrid

I'm trying to run a SQL report Query and get the result to a datagrid. My query works in SSMS, but in my code I get errors complaining about a '(' and 'as' I've gone through the code but can't seem to find the error.
Here is the part with the query:
myCmd = New SqlCommand("Declare #StartDate datetime," & _
"#StopDate datetime, " & _
"#location float; " & _
"set #StartDate='" & DateF & "'" & _
"set #StopDate='" & DateT & "'" & _
"set #Location = '1'" & _
"SELECT " & _
"MAX(CASE WHEN [Purchases].[Type]=1 OR [Purchases].[Type]=2 THEN -1 ELSE CONVERT(nvarchar,[LEVEL1].[Department Number]) END) as 'Department Number', " & _
"MAX(CASE WHEN [Purchases].[Type]=1 OR [Purchases].[Type]=2 THEN 'Unallocated'ELSE ISNULL([LEVEL1].[Department Name],'Deleted') END) as 'Department Name', " & _
"SUM(isnull([View_StockMovement].[Opening],0)) as 'Opening', " & _
"SUM(isnull([View_StockMovement].[Goods Received],0)) as 'Goods Received', " & _
"SUM(isnull([View_StockMovement].[Claims],0)) as 'Claims', " & _
"SUM(isnull([View_StockMovement].[Goods Received/Claim],0)) as 'Goods Received/Claim', " & _
"SUM(isnull([View_StockMovement].[Sales],0)) as 'Sales', " & _
"SUM(isnull([View_StockMovement].[Consumed],0)) as 'Consumed', " & _
"SUM(isnull([View_StockMovement].[Produced],0)) as 'Produced', " & _
"SUM(isnull([View_StockMovement].[Produced/Consumed],0)) as 'Produced/Consumed', " & _
"SUM(isnull([View_StockMovement].[Transfered],0)) as 'Transferred', " & _
"SUM(isnull([View_StockMovement].[Adjusted],0)) as 'Adjusted', " & _
"SUM(isnull([View_StockMovement].[Accepted],0)) as 'Accepted', " & _
"SUM(isnull([View_StockMovement].[Cost Adjustment],0)) as 'Cost Adjustment', " & _
"SUM(isnull([View_StockMovement].[Closing],0)) as 'Closing', " & _
"SUM(isnull([View_Sales Journal].[Gross Sales (Excl)],0)) as 'Gross Sales (Excl)', " & _
"SUM(isnull(Purchases.[Nett Purchase Value],0)) as 'Nett Cost Adjustment (Excl)', " & _
"SUM(isnull([View_StockMovement].[Opening],0) - isnull([View_StockMovement].[Closing],0) + isnull(Purchases.[Nett Purchase Value],0)) " & _
"as 'Operating Cost', " & _
"SUM(isnull([View_Sales Journal].[Gross Sales (Excl)],0) - (isnull([View_StockMovement].[Opening],0) - isnull([View_StockMovement].[Closing],0) + isnull(Purchases.[Nett Purchase Value],0))) " & _
"as 'Gross Operating Profit', " & _
"SUM(isnull([Cost Adjustment System],0)) as [Cost Adjustment System], " & _
"SUM(isnull([Cost Adjustment User], 0)) as [Cost Adjustment User] " & _
"From [product details] " & _
"LEFT OUTER JOIN [department details] as [LEVEL5] ON [LEVEL5].[department number]=[Product Details].[Department Number] " & _
"LEFT OUTER JOIN [department details] as [LEVEL4] ON [LEVEL4].[Department Number]=[LEVEL5].[Reporting Department] " & _
"LEFT OUTER JOIN [department details] as [LEVEL3] ON [LEVEL3].[Department Number]=[LEVEL4].[Reporting Department] " & _
"LEFT OUTER JOIN [department details] as [LEVEL2] ON [LEVEL2].[Department Number]=[LEVEL3].[Reporting Department] " & _
"LEFT OUTER JOIN [department details] as [LEVEL1] ON [LEVEL1].[Department Number]=[LEVEL2].[Reporting Department] " & _
"FULL(Join) " & _
"(SELECT [Type], [Product Code], " & _
"SUM([Nett Purchase Value]) AS 'Nett Purchase Value' " & _
"FROM dbo.fn_Table_PurchaseSummary(#StartDate, #StopDate,-1,-1,-1,-1) as [Consolidated] " & _
"GROUP BY [Consolidated].[Type],[Consolidated].[Product Code]) as [Purchases] ON [Product Details].[Product Code] = [Purchases].[Product Code] AND 0 = [Purchases].[Type] " & _
"LEFT OUTER JOIN " & _
"(SELECT [Sales Journal].[Product Code], " & _
"sum([Sales Journal].[Line Total] - [Sales Journal].[Sales Tax]) as 'Gross Sales (Excl)' " & _
"From [Sales Journal] WITH (NOLOCK) " & _
"WHERE [Sales Journal].[Function Key] in (4,5,6,7,8) AND [Sales Journal].[Date & _ Time] > #StartDate AND [Sales Journal].[Date & _ Time] < #StopDate " & _
"GROUP BY [Sales Journal].[Product Code]) as 'View_Sales Journal' ON [View_Sales Journal].[Product Code]=[Product Details].[Product Code] " & _
"LEFT OUTER JOIN dbo.fn_Table_StockMovement(#StartDate, #StopDate, #Location) as 'View_StockMovement' ON [product details].[product code]=[View_StockMovement].[product code] " & _
"WHERE (isnull(convert(tinyint, [Product Details].[Deleted]), 0) <> 1 AND " & _
"((isnull([View_StockMovement].[Opening],0) <> 0 or isnull([View_StockMovement].[Goods Received],0) <> 0 or " & _
"isnull([View_StockMovement].[Claims],0) <> 0 or isnull([View_StockMovement].[Goods Received/Claim],0) <> 0 or isnull([View_StockMovement].[Sales],0) <> 0 or " & _
"isnull([View_StockMovement].[Consumed],0) <> 0 or isnull([View_StockMovement].[Produced],0) <> 0 or " & _
"isnull([View_StockMovement].[Produced/Consumed],0) <> 0 or isnull([View_StockMovement].[Transfered],0) <> 0 or " & _
"isnull([View_StockMovement].[Adjusted],0) <> 0 or isnull([View_StockMovement].[Accepted],0) <> 0 or " & _
"isnull([View_StockMovement].[Cost Adjustment],0) <> 0 or isnull([View_StockMovement].[Closing],0) <> 0))) " & _
"GROUP BY [LEVEL1].[Department Number], isnull([Purchases].[Type], 0) ORDER BY [Department Name]", myConn)
Missing a trailing space on this line? This causes the query to run into the SELECT statement
"set #Location = '1'" & _

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

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"