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 & ") " & _
") "
Related
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;
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.
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"
Writing an sql string in vba and getting syntax error for a missing operator. I thought the error was with the inner joins so I tried taking them out but still same error.
Here is the query string:
sql1 = "SELECT WorkOrder.ProjectID, tref_dep.department, live_project.project_codename " _
& "FROM WorkOrder " _
& "INNER JOIN tlive_project " _
& "ON tlive_project.project_id = WorkOrder.ProjectID " _
& "INNER JOIN tref_dep " _
& "ON tref_dep.dep_id = WorkOrder.ToDepartment " _
& "WHERE WorkOrder.ToDepartment = " & rs1!wo_depart_id & " AND WorkOrder.ProjectID = " & rs1!proj_id _
& " CONTAINS(WorkOrder.WorkOrderDescription, 'TimeForce Upload,') " _
& "LIMIT 1"
I am probably missing something simple but any help is greatly appreciated!
You have forgotten a logic operator before CONTAINS(..) :
& "WHERE WorkOrder.ToDepartment = " & rs1!wo_depart_id & " AND WorkOrder.ProjectID = " & rs1!proj_id _
& "AND CONTAINS(WorkOrder.WorkOrderDescription, 'TimeForce Upload,') " _
& "LIMIT 1"
VBA requires parenthesis for 2 or more JOIN clauses.
sql1 = "SELECT WorkOrder.ProjectID, tref_dep.department, live_project.project_codename " _
& "FROM (WorkOrder " _
& "INNER JOIN tlive_project " _
& "ON tlive_project.project_id = WorkOrder.ProjectID) " _
& "INNER JOIN tref_dep " _
& "ON tref_dep.dep_id = WorkOrder.ToDepartment " _
& "WHERE WorkOrder.ToDepartment = " & rs1!wo_depart_id & " AND WorkOrder.ProjectID = " & rs1!proj_id _
& " CONTAINS(WorkOrder.WorkOrderDescription, 'TimeForce Upload,') " _
& "LIMIT 1"
I have tried and tried to get this peice of code working but with no luck.
The code is supposed to pick products that have the same sections of product code as cmbsource.
Private Sub cmbSource_AfterUpdate()
Dim Worktop As String
If ProductType = 1 Then
Worktop = "SELECT [products/stock].[Product Code], [products/stock].Description, [products/stock].[Stock Level] FROM [products/stock] " & _
"WHERE Category = 'DPALRC' " & _
"AND Mid('[Product Code]',1,5) = Mid('" & Me.cmbSource.Value & "',1,5) " & _
"AND Mid('[Product Code]',9,6) = Mid('" & Me.cmbSource.Value & "',9,6) " & _
"ORDER BY [Product Code];"
Me.cmbResult.rowSource = Worktop
Me.cmbResult = vbNullString
End If
Any Ideas?
Thanks in advance,
Bob P
Just a wild guess here.
Worktop = "SELECT ps.[Product Code], ps.Description, ps.[Stock Level]" & vbCrLf & _
"FROM [products/stock] AS ps" & vbCrLf & _
"WHERE ps.Category = 'DPALRC'" & vbCrLf & _
"AND Mid([Product Code],1,5) = '" & Mid(Me.cmbSource,1,5) & "'" & vbCrLf & _
"AND Mid([Product Code],9,6) = '" & Mid(Me.cmbSource,9,6) & "'" & vbCrLf & _
"ORDER BY ps.[Product Code];"
Debug.Print Worktop
Go to the Immediate window (Ctrl+g) and copy the statement from there. Then create a new Access query, switch to SQL View, paste in the statement text, and run it.
If the code I suggested does not return the results you want, show us the actual SQL statement text and explain what is wrong with it.
Worktop = "SELECT [products/stock].[Product Code], [products/stock].Description, [products/stock].[Stock Level] FROM [products/stock] " & _
"WHERE Category = 'DPALRC' " & _
"AND Mid([Product Code],1,5) = Mid('" & Me.cmbSource.Value & "',1,5) " & _
"AND Mid([Product Code],9,6) = Mid('" & Me.cmbSource.Value & "',9,6) " & _
"ORDER BY [Product Code];"
This is my updated version, turns out the only error in the coding was having the single quotes around [Product Code].