VBA sql string error: missing operator - sql

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"

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;

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

vba sql statement error "missing operator"

I can't get it work in VBA althought it works within access.
Which is the mistake?
Private Sub Command517_Click()
Dim SQL As String
SQL = "SELECT Tags.Tag, MacroAttività.[Nome/Descrizione], MacroAttività.ID," & _
" MacroAttività.Descrizione, MacroAttività.[Data inizio attività], " & _
" MacroAttività.[Data fine prevista], MacroAttività.[Data fine effettiva], " & _
" MacroAttività.Note,operatore.Nome, Tipologia.Tipologia " & _
" FROM Tipologia INNER JOIN (operatore INNER JOIN (MacroAttività INNER JOIN Tags ON MacroAttività.ID = Tags.[ID macro attività]) " & _
" ON operatore.ID = MacroAttività.Leader) ON Tipologia.ID = MacroAttività.Tipologia WHERE " & _
"(((Tags.Tag) = [Forms]![MacroAttività]![Text511]) " & _
" UNION ALL SELECT Tags.Tag, MacroAttività.[Nome/Descrizione], " & _
" MacroAttività.Descrizione, MacroAttività.ID, " & _
" MacroAttività.Leader, MacroAttività.[Data inizio attività], " & _
" MacroAttività.[Data fine prevista], MacroAttività.[Data fine effettiva], " & _
" MacroAttività.Note, MacroAttività.Tipologia " & _
" FROM MacroAttività INNER JOIN Tags ON MacroAttività.ID = Tags.[ID macro attività] " & _
" WHERE (((Tags.Tag)=[Forms]![MacroAttività]![Text513])); "
DoCmd.RunSQL SQL
End Sub
Form variables have to be read from outside of the SQL statement, so my solution closes the SQL statement adds the form variable and then reopens the SQL quotation. I have added string qualifiers(single quotes) around the form variables; assuming they are strings by the use of "text".
varSQL = "SELECT Tags.Tag, MacroAttività.[Nome/Descrizione], MacroAttività.ID," & _
" MacroAttività.Descrizione, MacroAttività.[Data inizio attività], " & _
" MacroAttività.[Data fine prevista], MacroAttività.[Data fine effettiva], " & _
" MacroAttività.Note,operatore.Nome, Tipologia.Tipologia " & _
" FROM Tipologia INNER JOIN (operatore INNER JOIN (MacroAttività INNER JOIN Tags ON MacroAttività.ID = Tags.[ID macro attività]) " & _
" ON operatore.ID = MacroAttività.Leader) ON Tipologia.ID = MacroAttività.Tipologia WHERE " & _
"(((Tags.Tag) ='" & [Forms]![MacroAttività]![Text511] & "')" & _
" UNION ALL SELECT Tags.Tag, MacroAttività.[Nome/Descrizione], " & _
" MacroAttività.Descrizione, MacroAttività.ID, " & _
" MacroAttività.Leader, MacroAttività.[Data inizio attività], " & _
" MacroAttività.[Data fine prevista], MacroAttività.[Data fine effettiva], " & _
" MacroAttività.Note, MacroAttività.Tipologia " & _
" FROM MacroAttività INNER JOIN Tags ON MacroAttività.ID = Tags.[ID macro attività] " & _
" WHERE (((Tags.Tag)='" & [Forms]![MacroAttività]![Text513] & "')); "
SQL is a reserved word so change your SQL variable name to something like varSQL. (Update: not a reserved word)
currentdb.execute and domcd.runSQL only work for action queries as far as I am aware. you cant execute a select query using these methods. try the following:
dim rs as recordset
dim varSQL as string
varSQL = "SELECT..."
set rs=currentdb.openrecordset(varSQL,dbOpenDynaset)
me.[text1] = rs.fieldname
'send recordset values to form
rs.close

Access 2007 Query Def Syntax Error

this is really frustrating and I'm hoping you can help me. I keep getting a syntax error no matter what I do on this query. I've read about putting parantheses on multiple joins but I did that and it doesn't work. What am I doing wrong here? Thanks for your time.
Set PTI = CurrentDb.CreateQueryDef("qry_PTImport", "SELECT DISTINCT" & _
" bl.LoanNumber" & _
", qcq.Code" & _
", t.ecType" & _
", eb.QueryNo" & _
", q.QueryName" & _
", q.Description" & _
", q.suggestedAction" & _
", bd.datetransfer" & _
", bd.datebrd " & _
"FROM ((((dbo.eCs_QCqueue qcq " & _
"JOIN dbo.eCs_Queries q ON q.QueryNo = eb.QueryNo) " & _
"JOIN brd_loan bl ON bl.serialID = qcq.serialID) " & _
"JOIN dbo.eCs_Type t ON t.ectypeid = q.ectypeid) " & _
"JOIN brd_deal bd ON bd.Code = bl.Code) " & _
"WHERE qcq.Code = '" & Deal & "'")
just wanted to say user HansUp was right in this instance of what my problem was. VBA doesn't do just JOINs.