All Works fine now! Thank you all for your help
I have been looking at the same line of code for the last two hours and don't understand why I continue to get the same Run Time Error 3067 message (input for the query must contain at least one table/query). This query is suppose to take three tables, linked with relationships and past 4 columns to a 4th table. The code below I copied directly from a query built in Access which worked..
DoCmd.SetWarnings False
st_sql = "INSERT INTO tblContactsProjectTrk01 ([Participant], [Sub_Project], [Role_type], [Completion_Percentage])" & _
"SELECT [tblProjManagementPhaseParticipants].[Participant], [tblProjectMasterList].[Sub project], [tblProjManagementPhaseParticipants].[Role_type], Avg([tblMasterListOfEvents].[Completion percentage]) AS MediaDiCompletion percentage" & _
"FROM[tblMasterListOfEvents] INNER JOIN ([tblProjManagementPhaseParticipants] INNER JOIN [tblProjectMasterList] ON [tblProjManagementPhaseParticipants].[ID_Project] = [tblProjectMasterList].[ID Project]) ON ([tblProjectMasterList].[ID Project] = [tblMasterListOfEvents].[ID Project]) AND ([tblMasterListOfEvents].[ID Event] = [tblProjManagementPhaseParticipants].[ID_Event])" & _
"GROUP BY([tblProjManagementPhaseParticipants].[Participant], [tblProjectMasterList].[Sub project], [tblProjManagementPhaseParticipants].[Role_type])" & _
"ORDER BY[tblProjManagementPhaseParticipants].[Participant]"
Application.DoCmd.RunSQL (st_sql)
There is no space before the FROM, so it is actually percentageFROM. I think it should be
...Avg([tblMasterListOfEvents].[Completion percentage]) AS [MediaDiCompletion percentage] " & _
"FROM[tblMasterListOfEvents]...
You have the same problem with your line wrapping before your GROUP BY and ORDER BY statements. You need spaces between the previous line and those two statements.
Related
Let's say I've got this query containing multiple tables.
Containing this data when run :
I want the query to show me the data exactly as it is in the database but when I run my SQL statement it'll show it as if on the second row the field OpslagLocatie has the same data as the field above it.
Outcome :
My solution :
Me.[Keuzelijst EAN artikel op vooraad].RowSource =
"SELECT TussenMAATenARTKEL.EAN_Code AS [EAN Code], TbOpzoekKleur.Kleur AS Kleur, TbOpzoekVoorraadMaat.VoorraadMaten AS Maat, Sum(Stock.Aantal_per_Lokatie) AS Aantal, TbOpzoekVoorraadLocatie.VoorraadLocatie AS Lokatie, Stock.ArtikelDetail_ID AS Detail, IIf([TussenMAATenARTKEL].[Opslaglocatie], [Stock].[VoorraadLocatie_ID]=8,[TussenMAATenARTKEL].[OpslagLocatie]) AS [Opslag Locatie] " & _
"FROM (TbOpzoekVoorraadMaat RIGHT JOIN (TbOpzoekKleur RIGHT JOIN TussenMAATenARTKEL ON TbOpzoekKleur.ColorCode = TussenMAATenARTKEL.Colorcode) ON TbOpzoekVoorraadMaat.VoorraadNummer = TussenMAATenARTKEL.VoorraadNummer) RIGHT JOIN (TbOpzoekVoorraadLocatie RIGHT JOIN Stock ON TbOpzoekVoorraadLocatie.VoorraadLocatie_ID = Stock.VoorraadLocatie_ID) ON TussenMAATenARTKEL.ArtikelDetail_ID = Stock.ArtikelDetail_ID " & _
"GROUP BY TussenMAATenARTKEL.EAN_Code, TbOpzoekKleur.Kleur, TbOpzoekVoorraadMaat.VoorraadMaten, TbOpzoekVoorraadLocatie.VoorraadLocatie, Stock.ArtikelDetail_ID, Stock.VoorraadLocatie_ID, TussenMAATenARTKEL.Artikel_ID, TussenMAATenARTKEL.OpslagLocatie " & _
"HAVING (((Sum(Stock.Aantal_per_Lokatie)) <> 0) And ((TussenMAATenARTKEL.Artikel_ID) = " & test & ")) " & _
"ORDER BY TbOpzoekVoorraadMaat.VoorraadMaten;"
Outcome after updating my code with an iif statement :
After running my code the field TussenMAATenARTKEL.OpslagLocatie only shows the values -1 or 0. I expect it to show or not show the OpslagLocatie instead of -1 and 0.
What am I doing wrong?
That's because your OpslagLocatie is a lookup field.
So, in your query, join and include the table and the field in this from where OpslagLocatie looks up the code KBA-ACC.
I am trying to find the most optimized way to do this operation which runs an SQL statement in Access-VBA :
SQl = "UPDATE " _
& "MainTable As T1 " _
& "INNER JOIN TableOfLists As T2 " _
& "ON (T2.SecondList = T1.MultiValuedList.value) " _
& "Set [FOUND IN LISTS] = 'YES' "
DoCmd.RunSQL SQl
SQl = "UPDATE " _
& "MainTable As T1 " _
& "INNER JOIN TableOfLists As T2 " _
& "ON (T2.FirstList = T1.MultiValuedList.value) " _
& "Set [FOUND IN LISTS] = 'YES' "
DoCmd.RunSQL SQl
This code works, can be improved surely, but I didn't manage to find out how.
What I've tried so far and the results I got :
Adding 2 INNER JOIN but I get a syntax error 3075
Adding 2 conditions separated by an OR in the INNER JOIN condition but I get an error 3081 : can't join more than 1 table
This was my previous solution using 2 SELECT statements but I
got recommended to use JOIN instead
Any suggestions welcomed !
Here you go. This is kind of the ANSI SQL way of doing things, because joins within an UPDATE are not supported. (See this blog entry for more detail.) This is why you saw an error in your first approach (with two INNER JOINs, because Access/Jet is giving you a special feature in their UPDATE syntax, but it's not as fully-developed as vanilla SQL is. Your second approach (with an OR in the join condition) errors out because Access/Jet's support for conditions in join criteria is very limited (you wouldn't see this in Oracle or Postgres, for example). And it turns out your third approach (with two selects, but using IN instead of EXISTS) is the same, under the hood. So whoever told you to use joins was ill-informed :).
UPDATE MainTable SET [FOUND IN LISTS]='YES'
WHERE
EXISTS (SELECT 1 FROM TableOfLists WHERE FirstList=MainTable.[value])
OR
EXISTS (SELECT 1 FROM TableOfLists WHERE SecondList=MainTable.[value]);
Example in Access
I got a syntax error when I ran a sql update in access 2013. The targeted table saves inventory balance data. to update the balance, the original data will subtract the consumption amount resulted from production activities. I use a query to get the sum of consumption. My codes are as follows:
Private Sub Command4_Click()
CurrentDb.Execute ("UPDATE tbl_Current_Stock As o" & _
“Inner join preview_Of_Raw_Material_Consumption As p” & _
“On o.Raw_Material =p.[Ingredient/Packaging material]” & _
"Set o.Stock_Level = o.Stock_Level- p.SumOfConsumption" )
End Sub
EDIT - Add current code that isn't working:
CurrentDb.Execute ("UPDATE tbl_Current_Stock As o Inner join preview_Of_Raw_Material_Consumption As p On o.[Raw_Material] = p.[Ingredient/Packaging material]Set o.[Stock_Level] = o.[Stock_Level]- p.SumOfConsumption from o p")
Your query looks something like this:
UPDATE tbl_Current_Stock As oInner join preview_Of_Raw_Material_Consumption As pOn o.Raw_Material =p.[Ingredient/Packaging material]Set o.Stock_Level = o.Stock_Level- p.SumOfConsumption
Can you spot the problem? Yes, you have run-on lines in the query. If you print the query string before running it, then the problem is obvious.
The solution is to put appropriate spaces in the string:
CurrentDb.Execute ("UPDATE tbl_Current_Stock As o" & _
" Inner join preview_Of_Raw_Material_Consumption As p” & _
" On o.Raw_Material = p.[Ingredient/Packaging material]” & _
" Set o.Stock_Level = o.Stock_Level- p.SumOfConsumption" )
Thank you Gordon. I finaly found the real problem of my code. I would like to share my experiences and it maybe helpful for others who are bothered by the same or similar problem. As I mentioned in my first question post, one of my update source comes from a query. this query is a sum of individual consumption and it is "unupdatable". By searching online I use a temporal table as the data source and the code runs smoothly.
CurrentDb.Execute ("Update tbl_Current_Stock As o Inner join tbl_Temp_Raw_Material_Consumption As s On o.Raw_Material = s.[Ingredient/Packaging material] Set o.Stock_Level = o.Stock_Level - s.Consumption")
Here tbl_Temp_Raw_Material_Consumption is the make table query of the original query.
I need a second set of eyes on this SQL query embedded in the VBA code. I'm making an MS Access app that returns a dataset based on the to & from date criteria set by the user in the specific date picker boxes. The sql query that you see actually has been tested statically within MS Access query design view. I tested it with actual dates where you see the Me.from_filter and Me.to_filter. It worked perfectly! If you chose something like from 1/1/2015 to 5/1/2015 it returns columns of all the months you need. Perfect. Now when I embed it in the VBA code and assign it to a control variable, I get the "Run-time error '2342': A RunSQL action requires an argument consisting of an SQL statement." Can someone please eyeball this and tell me what might be wrong?
Option Compare Database
Option Explicit
Dim strSQL As String
Private Sub Command0_Click()
If IsNull(Me.from_filter) Or IsNull(Me.to_filter) Then
MsgBox "You have not entered a start date or end date"
Else
strSQL = "TRANSFORM Sum(dbo_ASSET_HISTORY.MARKET_VALUE) AS SumOfMARKET_VALUE " _
& "SELECT [dbo_FIRM]![NAME] AS [FIRM NAME], dbo_FUND.CUSIP, dbo_FUND.FUND_NAME, dbo_FUND.PRODUCT_NAME " _
& "FROM (dbo_ASSET_HISTORY INNER JOIN dbo_FIRM ON dbo_ASSET_HISTORY.FIRM_ID = dbo_FIRM.FIRM_ID) INNER JOIN dbo_FUND ON dbo_ASSET_HISTORY.FUND = dbo_FUND.FUND " _
& "WHERE (((dbo_FIRM.Name) Like 'Voya F*') And ((dbo_ASSET_HISTORY.PROCESS_DATE) >= #" & Me.from_filter & "# And (dbo_ASSET_HISTORY.PROCESS_DATE) <= #" & Me.to_filter & "#)) " _
& "GROUP BY [dbo_FIRM]![NAME], dbo_FUND.CUSIP, dbo_FUND.FUND_NAME, dbo_FUND.PRODUCT_NAME " _
& "PIVOT [dbo_ASSET_HISTORY]![ASSET_YEAR] & '-' & [dbo_ASSET_HISTORY]![ASSET_MONTH];"
DoCmd.RunSQL (strSQL)
End If
End Sub
RunSQL is for running action queries like update, insert, select into, delete, etc as per Microsoft definition https://msdn.microsoft.com/en-us/library/office/ff194626.aspx , you should probably use OpenQuery or similar to run your query.
I am looking to update one of my tables with another table depending on a field condition. I am wondering what the correct way to do this is. I tried to do UPDATE statements with the 2 tables, but everytime it came out with this error: Too few Parameters, Expected 2
My goal:
If SOURCING field = O, I want Zip Code to be Origin Postal Code
If SOURCING field = D, I want Zip Code to be Dest Postal Code
So as of right now, I am simply doing a LEFT JOIN with a condition. Is this the best way to do it? Or should I have done this with the original INSERT statement somehow?
CurrentDb.Execute "UPDATE Processing" & _
" LEFT JOIN tblImport" & _
" ON Processing.[BATCH_NO] = tblImport.[BATCH_NO]" & _
" SET Processing.[Zip Code] = tblImport.[Origin Postal Code]" & _
" WHERE tblImport.[Sourcing] = O;"
CurrentDb.Execute "UPDATE Processing" & _
" LEFT JOIN tblImport" & _
" ON Processing.[BATCH_NO] = tblImport.[BATCH_NO]" & _
" SET Processing.[Zip Code] = tblImport.[Dest Postal Code]" & _
" WHERE tblImport.[Sourcing] = D;"
I have tried changing the WHERE statement since I am not sure if it should be in quotes, single quotes, not in quotes, etc... but I have come up empty there. Everything else looks correct to me.
Here's what I do, you might find this helpful; Set a variable equal to your SQL string, then CurrentDB.Execute the variable. Why is this helpful? Because you can break the code after the variable is set, and then copy the SQL into a new query and see what it's complaining about. :o)
Dim tmpUpdate as string
tmpUpdate = "UPDATE Processing" & _
" LEFT JOIN tblImport" & _
" ON Processing.[BATCH_NO] = tblImport.[BATCH_NO]" & _
" SET Processing.[Zip Code] = tblImport.[Origin Postal Code]" & _
" WHERE tblImport.[Sourcing] = O;"
CurrentDB.Execute tmpUpdate
Set a breakpoint on the "tmpUpdate =" line, and then run the code. When it hits the breakpoint, press F8 to step to the next line. In the Immediate window, type "?tmpUpdate" (without the quotes) and see what it thinks the variable is equal to. Then, go to the database, create a new query and go to the SQL of the query. Copy and paste the SQL from the Immediate window and try running the query. If it pukes, go to the Design view and see if you can see anything that doesn't look right.