Optimize SQL query in VBA - sql

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

Related

Access VBA 'Syntax Error in Join Operation'

I'm trying to attach a SELECT query to a VBA object in Access, but I keep getting the error:
Syntax Error in Join operation
Dim sql As String
sql = "SELECT R_History.iteration, R.RuntimeID, Policylanguage.FormBody " & _
"FROM PolicyLanguage " & _
"INNER JOIN ([Policy&Elements] " & _
"INNER JOIN ((R " & _
"INNER JOIN R_History " & _
"ON (R.RunTimeID = R_History.RunTimeID " & _
"AND (R.ReportID = R_History.ReportID)) " & _
"INNER JOIN StatePolicyLanguage " & _
"ON R_History.Iteration = StatePolicyLanguage.Iteration) " & _
"ON [Policy&Elements].[Text&ElementID] = StatePolicyLanguage.[TextID&ElementID] " & _
"ON PolicyLanguage.TextID = [Policy&Elements].TextID " & _
"WHERE (((R.RuntimeID) = Dlast('runtimeID','r')));"
DoCmd.RunSQL (sql)
I can't get this to run for the absolute life of me. Can anyone tell what's wrong?
Edit: Here is the code I am trying to emulate
If you're running a query where the criteria are values entered in a form (e.g. field1 = frm1.txtValue), you could create a parameterized query where the passed parameters are added to the WHERE clause. That way, you don't have create SQL in VBA---you'd just have to set the parameters in VBA.
As noted by others, Access requires you to put parentheses after each join:
from (((Table1
inner join Table2 on Table2.field1 = Table1.field1)
inner join Table3 on Table3.field2 = Table2.field2)
inner join Table4 on Table4.field3 = Table3.field3)
So, as suggested by others, if you absolutely need to construct the query via VBA, keep your JOIN and ON clauses together to prevent confusion, and add the necessary parentheses.
I've found when you have to create complex queries in Access (either in the SQL editor or in VBA), it can be helpful to write the SQL in a more friendly editor (e.g. Notepad++) that supports things like syntax highlighting. Then, you can paste the query where it needs to go.

syntax error for update sql 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.

Operator/Syntax Error

I am executing SQL in VB6 and this is the string that I am using, where I define currFA as a number 1. I've been debugging and working at this since it's using inner joins and unions (it's a query I made in Access that I am trying to put into VB6 to run). Right now, I'm getting an operator missing error on the run.
sql = "SELECT DISTINCT [~ALLCBLEQ].Equipment, [~FIRE_AREAS].INDEX" _
& "FROM ([~FIRE_AREAS] INNER JOIN [~ALLTARGETS] ON [~FIRE_AREAS].FULL_ID = [~ALLTARGETS].FA) INNER JOIN ([~ALLRWCBL] INNER JOIN [~ALLCBLEQ] ON" _
& "[~ALLRWCBL].Cable = [~ALLCBLEQ].Cables) ON [~ALLTARGETS].TARGET = [~ALLRWCBL].Cable" _
& "WHERE ((([~FIRE_AREAS].INDEX) = '" & currFA & "'))" _
& "UNION" _
& "SELECT DISTINCT [~ALLCBLEQ].Equipment, [~FIRE_AREAS].INDEX" _
& "FROM [~FIRE_AREAS] INNER JOIN (([~ALLTARGETS] INNER JOIN [~ALLRWCBL] ON [~ALLTARGETS].TARGET=[~ALLRWCBL].Raceway) INNER JOIN [~ALLCBLEQ] ON" _
& "[~ALLRWCBL].Cable=[~ALLCBLEQ].Cables) ON [~FIRE_AREAS].FULL_ID=[~ALLTARGETS].FA" _
& "WHERE ((([~FIRE_AREAS].INDEX) = '" & currFA & "'));"
Any help on the error and syntax improvement tips are greatly appreciated! Still learning!
Thanks!
Your string has no spaces between the lines so when you write:
sql = "SELECT DISTINCT [~ALLCBLEQ].Equipment, [~FIRE_AREAS].INDEX" _
& "FROM ([~FIRE_AREAS] …
That becomes a SQL string like
SELECT DISTINCT [~ALLCBLEQ].Equipment, [~FIRE_AREAS].INDEXFROM ([~FIRE_AREAS] …
Which is obviously invalid SQL because there's no space between INDEX and FROM.
An easy solution is just to put a space before each closing quote (or after each open quote):
sql = "SELECT DISTINCT [~ALLCBLEQ].Equipment, [~FIRE_AREAS].INDEX " _
& "FROM ([~FIRE_AREAS] …
However, If you find yourself writing relatively complex queries like this, I would recommend that you consider converting them to stored procedures or views instead.

run time error 3067 in Access with SQL query

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.

Join operation syntax error

I am trying to make a query through a button; though it says
Syntax error in JOIN operation. When I click it... I can't find the syntax error, maybe you guys can help me out? This is the code:
Set qdef = CurrentDb.CreateQueryDef("UnitMoreInfoQ", _
"SELECT UnitsT.*, WorkOrdersQ.CustomerName, WorkOrdersQ.ClientName, WorkOrdersQ.WorkOrderNumber " & _
"FROM UnitsT inner join workordersQ on WorkOrdersT.WorkOrerID=WorkOrdersQ.WorkOrderID " & _
"WHERE UnitsT.UnitID = " & txtWorkOrderID.Value)
The problem seems to be with your JOIN condition:
WorkOrdersT.WorkOrerID=WorkOrdersQ.WorkOrderID
There isn't a WorkOrdersT table or table alias defined in the FROM or other join, so your query isn't valid.
Make it easier on yourself to find and fix SQL errors.
Use a string variable to hold the SQL statement your code constructs. You can Debug.Print that statement before using it with CreateQueryDef. Then when troubleshooting, go to the Immediate window (Ctrl+g) to examine the statement your code is attempting to use. You can copy it from there and then paste it into SQL View of a new query for further testing. And if you need help, show us the completed statement text instead of the VBA code which builds the statement.
Dim strSelect As String
strSelect = "SELECT u.*, w.CustomerName, w.ClientName, w.WorkOrderNumber " & _
"FROM UnitsT As u inner join workordersQ AS w " & _
"on u.WorkOrerID=w.WorkOrderID " & _
"WHERE u.UnitID = " & txtWorkOrderID.Value
Debug.Print strSelect
Set qdef = CurrentDb.CreateQueryDef("UnitMoreInfoQ", strSelect)
Alternatively, use the query designer to create the query you need from scratch. Once you get that working, switch to SQL view, copy the statement text, and revise your VBA code to create the same statement.