syntax error (missing operator) in query expression - VBA and Access - sql

I build a query with this syntax:
SELECT e.codigo AS `Código`,
e.razao_social AS `Razão Social`,
e.grupo AS `Grupo`,
e.tributacao AS `Tributação`,
e.sistema AS `Sistema`,
r.nome AS `Responsável`,
date_format(t.competencia, '%m/%Y') AS `Competência`,
s.nome AS `Status`,
c.nome AS `Tipo Conferência`
FROM tarefa AS t
RIGHT JOIN empresa AS e ON t.id_empresa = e.id_empresa
LEFT JOIN responsavel AS r ON t.id_responsavel = r.id_responsavel
LEFT JOIN status AS s ON t.id_status = s.id_status
LEFT JOIN conferencia AS c ON t.id_conferencia = c.id_conferencia
WHERE c.nome = 'Encerramento Contábil'
ORDER BY `Competencia`;
I did a test query in MariaDB, and it worked.
Now, i am use this query in Access with VBA MSExcel, where it has the same structure and relationship between tables, but return error.
message error vba:
Syntax error (missing operator) in expression 't.id_empresa =
e.id_empresa LEFT JOIN responsavel AS r ON t.id_respons'
This is my code vba MSExcel:
Sub testSql()
'Creating objects of Connection and Recordset
Dim conn As New Connection, rec As New Recordset
Dim DBPATH, PRVD, connString, query As String
'Declaring fully qualified name of database. Change it with your database's location and name.
DBPATH = "C:\Users\ctb06\Documents\Database2.accdb"
'This is the connection provider. Remember this for your interview.
PRVD = "Microsoft.ace.OLEDB.12.0;"
'This is the connection string that you will require when opening the the connection.
connString = "Provider=" & PRVD & "Data Source=" & DBPATH
'opening the connection
conn.Open connString
'the query I want to run on the database.
query = "SELECT e.codigo AS `Código`, e.razao_social AS `Razão Social`, e.grupo AS `Grupo`, e.tributacao AS `Tributação`, e.sistema AS `Sistema`, r.nome AS `Responsável`, date_format(t.competencia, '%m/%Y') AS `Competência` FROM tarefa AS t RIGHT JOIN empresa AS e ON t.id_empresa = e.id_empresa LEFT JOIN responsavel AS r ON t.id_responsavel = r.id_responsavel;"
'running the query on the open connection. It will get all the data in the rec object.
rec.Open query, conn
'clearing the content of the cells
Range("a1").Select
Cells.ClearContents
If (rec.RecordCount <> 0) Then
col = 1
For Each resp In rec.Fields
With Cells(1, col)
.Value = resp.Name
End With
col = col + 1
Next resp
Cells(2, 1).CopyFromRecordset rec
End If
rec.Close
conn.Close
End Sub
Where am I missing?

No two SQL dialects are exactly the same for exact transferability. Simply, MariaDB's SQL will not perfectly align to MS Access' SQL similar to running same query from Oracle to Postgres, MySQL to SQL Server, Sybase to DB2... There will need to be some translation.
Specifically:
DATE_FORMAT is not an available function in MS Access. Instead use FORMAT with appropriate format pattern which does not use %.
More than one JOIN require parentheses wrapping in MS Access. However, your mix of RIGHT JOIN and LEFT JOIN may require nested joining.
(Admittedly, this is a frustrating requirement for new Access users to build complex queries with Query Designer and not SQL. I raised this suggested change among others to Access' SQL dialect.)
Fortunately, backticks are supported in MS Access though square brackets, [...], are the more popular form to escape identifiers with special characters or keywords.
Consider following adjustment:
SELECT e.codigo AS `Código`,
e.razao_social AS `Razão Social`,
e.grupo AS `Grupo`,
e.tributacao AS `Tributação`,
e.sistema AS `Sistema`,
r.nome AS `Responsável`,
FORMAT(t.competencia, 'mm/yyyy') AS `Competência`,
s.nome AS `Status`,
c.nome AS `Tipo Conferência`
FROM ((((empresa AS e
RIGHT JOIN tarefa AS t ON t.id_empresa = e.id_empresa)
LEFT JOIN responsavel AS r ON t.id_responsavel = r.id_responsavel)
LEFT JOIN status AS s ON t.id_status = s.id_status)
LEFT JOIN conferencia AS c ON t.id_conferencia = c.id_conferencia)
WHERE c.nome = 'Encerramento Contábil'
ORDER BY `Competencia`;

Related

Complex JOINS in Access SQL difficult to convert to JET OLEDB

I'm a long time follower of Stack overflow but this is my first post. I'm hoping the community can help.
I have a successful Access Query that returns the required results - Perfect!
HOWEVER, I'm trying to return the same using OLEDB connection to the database within an ASP script. This is all legacy stuff however we are allowing web access to this legacy information.
MS Access (2016) shows Query as this... (works)
SELECT [EventName] & ": " & [RoundCaption] AS RoundTitle, ChunkEntryTable.WinPos
FROM ((EventTable INNER JOIN EventRoundTable ON EventTable.EventId = EventRoundTable.EventId) INNER JOIN ((RoundHeatTable INNER JOIN ChunkTable ON RoundHeatTable.RoundHeatId = ChunkTable.RoundHeatId) INNER JOIN (EventEntryTable INNER JOIN ChunkEntryTable ON EventEntryTable.EventEntryId = ChunkEntryTable.EventEntryId) ON ChunkTable.ChunkId = ChunkEntryTable.ChunkId) ON EventRoundTable.RoundKeyId = RoundHeatTable.RoundKeyId) LEFT JOIN EventEntryMemberTable ON EventEntryTable.EventEntryId = EventEntryMemberTable.EventEntryId
WHERE (((EventEntryTable.Entry1Id)=[EntryId])) OR (((EventEntryTable.Entry2Id)=[EntryId])) OR (((EventEntryTable.Entry3Id)=[EntryId])) OR (((EventEntryMemberTable.MemberId)=[EntryId]))
ORDER BY EventTable.SortIdx, EventRoundTable.RoundId DESC , EventRoundTable.IsRepechage DESC;
Doing this in OLEDB. Connection string as follows...
<%
' FileName="Connection_ado_conn_string.htm"
' Type="ADO"
' DesigntimeType="ADO"
' HTTP="true"
' Catalog=""
' Schema=""
Dim MM_csresultdb_STRING
MM_csresultdb_STRING = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=xyz.mde;Jet OLEDB:Database Password=xxxxxxxxx;"
%>
Connection works perfectly but I can't seem to get the SQL command to work. I get "No value given for one or more required parameters".
NOTE: I have replaced [EntryID] in 4 places with a valid value and it works perfectly in Access just not outside of Access using OLEDB. Here's what the SQL is I'm using...
SELECT EventTable.EventName & ": " & EventRoundTable.RoundCaption AS RoundTitle, ChunkEntryTable.WinPos FROM
((EventTable INNER JOIN EventRoundTable ON EventTable.EventId = EventRoundTable.EventId) INNER JOIN
((RoundHeatTable INNER JOIN ChunkTable ON RoundHeatTable.RoundHeatId = ChunkTable.RoundHeatId) INNER JOIN
(EventEntryTable INNER JOIN ChunkEntryTable ON EventEntryTable.EventEntryId = ChunkEntryTable.EventEntryId) ON ChunkTable.ChunkId = ChunkEntryTable.ChunkId) ON ChunkTable.ChunkId = ChunkEntryTable.ChunkId)
ON EventRoundTable.RoundKeyId = RoundHeatTable.RoundKeyId)
WHERE ((EventEntryTable.Entry1Id)=4741) OR ((EventEntryTable.Entry2Id)=4741) OR ((EventEntryTable.Entry3Id)=4741)
ORDER BY EventTable.SortIdx, EventRoundTable.RoundId DESC , EventRoundTable.IsRepechage DESC;
FOUND PROBLEM ** See answer below
FOUND PROBLEM ** It's to do with this part of the SQL...
[EventName] & ": " & [RoundCaption] AS RoundTitle
Changed to
[EventName], [RoundCaption] AS RoundTitle
and it works but gives me two separate fields rather than the one concatenated field called "RoundTitle". So I'll join the two result fields during the display output rather than at the query stage.
Whew! That many days to figure out. Thanks to the comments that kinda steered me in that direction of the AS part of the statement.

select in select sql query

I want to create a sql query. The column name that I want, it's in another table. I wrote this query.
SELECT (SELECT FieldName From TableGENQuest WHERE ID = 1)
FROM TableGEN
WHERE strSO = 'RV12648-01';
I want to get the data from the strGEN1 columns using the FieldName column of the TableGENQuest table.That is data I want No significant transportation damage observed.
tl;dr: Your request is not possible using MS Access SQL alone.
You will need to use VBA to open a recordset containing the content of the table TableGENQuest and construct an appropriate SQL statement whilst iterating over the records held by such recordset.
For example:
Sub GenerateQuery()
Dim dbs As DAO.Database
Dim rst As DAO.Recordset
Dim sql As String
Set dbs = CurrentDb
Set rst = dbs.OpenRecordset("SELECT FieldName FROM TableGENQuest WHERE ID = 1")
With rst
If Not .EOF Then
.MoveFirst
Do Until .EOF
sql = sql & "[" & !FieldName & "], "
.MoveNext
Loop
sql = "select " & Left(sql, Len(sql) - 2) & " from TableGEN WHERE strSO = 'RV12648-01';"
End If
.Close
End With
If sql <> vbNullString Then dbs.CreateQueryDef "OutputQuery", sql
Set rst = Nothing
Set dbs = Nothing
End Sub
The above will generate a query defined in the current database with a SQL statement selecting the fields whose fieldnames are sourced using the SQL:
SELECT FieldName FROM TableGENQuest WHERE ID = 1
The difficulty and convoluted nature of this method indicates that your database is poorly designed: the field names themselves should instead appear as rows within another table.
SELECT b.FieldName FROM TableGEN a
LEFT OUTER JOIN TableGENQuest b
ON a.ID=b.ID
WHERE a.strSO = 'RV12648-01'
AND b.ID=1
OR
SELECT a.strSO,b.FieldName,b.ID
FROM TableGEN a
LEFT OUTER JOIN
(
SELECT FieldName,ID From TableGENQuest WHERE ID = 1
)
b
ON a.ID = b.ID
WHERE strSO = 'RV12648-01'
Try This Query...or can change Join Type...
Try this:
SELECT TableGENQuest.FieldName
FROM TableGEN, TableGENQuest
WHERE TableGENQuest.ID=1 AND WHERE strSO = 'RV12648-01';

MS Access using forms in a pass-through query

I have a form named FietsAantDagen, a query named QueryFietsAantDagen and a textbox named Txtinput. I am trying to use a pass-through query to SQL Server and use a text form's input as a input in my query.
Query:
SELECT
Fiets_id,
Fiets_Type,
SUM(DATEDIFF(DAY, Huurovereenkomst_Begin_datum, Huurovereenkomst_Eind_datum)) AS AantalDagen
FROM
Fiets
INNER JOIN
HuurovereenkomstFiets
ON HuurovereenkomstFiets_Fiets_id = Fiets_id
INNER JOIN
Huurovereenkomst
ON Huurovereenkomst_id = HuurovereenkomstFiets_Huurovereenkomst_id
WHERE
YEAR(Huurovereenkomst_Begin_datum) = [Forms]![FietsAantDagen]![Txtinput]
AND YEAR(Huurovereenkomst_Eind_datum) = [Forms]![FietsAantDagen]![Txtinput]
GROUP BY
Fiets_id,
Fiets_Type
While running this query as a pass-through query I get the following error:
ODBC: Runtime error [Microsoft][SQL Server Native Client 11.0][SQL
Server]Incorrect syntax near the keyword 'WHERE'/ (#156)
Is the problem that I am using an Access text form value in a pass-through query, if so what can I do to solve it?
I read in another Overflow question you needed to add (), which I did and now I get the error:
JOIN-expression is not supported.
I'm going crazy...
If you don’t have direct use of SQL server, then likely best to create two pass though queries.
Query #1 – this is your raw SQL as you have
Eg:
SELECT
Fiets_id,
Fiets_Type,
SUM(DATEDIFF(DAY, Huurovereenkomst_Begin_datum,
Huurovereenkomst_Eind_datum)) AS AantalDagen
FROM
Fiets
INNER JOIN
HuurovereenkomstFiets
ON HuurovereenkomstFiets_Fiets_id = Fiets_id
INNER JOIN
Huurovereenkomst
ON Huurovereenkomst_id = HuurovereenkomstFiets_Huurovereenkomst_id
WHERE
YEAR(Huurovereenkomst_Begin_datum) = [StartYear]
AND YEAR(Huurovereenkomst_Eind_datum) = [EndYear]
GROUP BY
Fiets_id,
Fiets_Type
Query #2 – this is an application wide query you make that you can re-use over and over for any raw t-SQL (SQL server pass though). You then in code go like this:
Dim rst As DAO.Recordset
Dim strSQL As String
strSQL = CurrentDb.QueryDefs("MyQ1").SQL
srtSQL = Replace(strSQL, "[YearStart]", [Forms]![FietsAantDagen]![Txtinput])
strSQL = Replace(strSQL, "[YearEnd]", [Forms]![FietsAantDagen]![Txtinput])
With CurrentDb.QueryDefs("qryPassR")
.SQL = strSQL
.ReturnsRecords = True
Set rst = .OpenRecordset
End With
However, if you have the ability to use SQL server, and create a proc, then I would suggest you create store procedure like this:
CREATE PROCEDURE SelectDates
#StartYear int,
#EndYear int
AS
BEGIN
SET NOCOUNT ON;
SELECT
Fiets_id,
Fiets_Type,
SUM(DATEDIFF(DAY, Huurovereenkomst_Begin_datum, Huurovereenkomst_Eind_datum)) AS AantalDagen
FROM
Fiets
INNER JOIN
HuurovereenkomstFiets
ON HuurovereenkomstFiets_Fiets_id = Fiets_id
INNER JOIN
Huurovereenkomst
ON Huurovereenkomst_id = HuurovereenkomstFiets_Huurovereenkomst_id
WHERE
YEAR(Huurovereenkomst_Begin_datum) = #StartYear
AND YEAR(Huurovereenkomst_Eind_datum) = #EndYear
GROUP BY
Fiets_id,
Fiets_Type
END
Then in access, you use this:
Dim rst As DAO.Recordset
With CurrentDb.QueryDefs("qryPassR")
.SQL = "exec SelectDates " & [Forms]![FietsAantDagen]![Txtinput] & "," & _
[Forms]![FietsAantDagen]![Txtinput]
.ReturnsRecords = True
Set rst = .OpenRecordset
End With
So by passing parameters you reduce most of issues in regards to SQL injection.
Of course if you can’t create store procs, or don’t have permissions, then you have to adopt the first idea above. You can also of course in the first suggestion insert the raw SQL into the code editor, but I find using up an extra query to “just” hold the raw SQL, and then modifying the SQL into the 2nd pass-through query eliminates the need for messy SQL in the VBA code editor.
When you run a Pass-Through query, its SQL text gets sent to the DB server as-is, nothing is evaluated. So including [Forms]![FietsAantDagen]![Txtinput] in a Pass-Through query will never work.
You're on the right track with
SELECT * FROM fnFietsAantDagenPerJaar([Forms]![FietsAantDagen]![Txtinput])‌​
but you need to supply the parameter yourself, because (see above).
So you need to build the Pass-Through SQL, e.g. with this VBA procedure:
Private Sub txtInput_AfterUpdate()
Dim QD As DAO.QueryDef
Dim lYear As Long
' Make sure we pass a number to the SQL Server function
lYear = Val(Me!txtInput)
If lYear > 0 Then
Set QD = CurrentDb.QueryDefs("QueryFietsAantDagen")
QD.SQL = "SELECT * FROM fnFietsAantDagenPerJaar(" & lYear & ")"
Set QD = Nothing
' After changing the query SQL, requery the form
' This assumes that your form is bound to Pass-Through query QueryFietsAantDagen
Me.Requery
End If
End Sub
Pass-through queries run exclusively on the connected, external database engine, here being SQL Server, and does not see anything in MS Access, only everything in that external environment.
Consider adjusting pass-through query to avoid any MS Access evaluations and add year groupings to aggregate query. Then query this resultset in a local Access query filtering for needed year conditions using form values. Below adds table aliases for clarity (adjust if not correct).
Pass-Through (no WHERE clause, but extended SELECT and GROUP BY)
SELECT
f.Fiets_id,
f.Fiets_Type,
YEAR(h.Huurovereenkomst_Begin_datum) As BeginYear,
YEAR(h.Huurovereenkomst_Eind_datum) As EndYear,
SUM(DATEDIFF('d', h.Huurovereenkomst_Begin_datum,
h.Huurovereenkomst_Eind_datum)) AS AantalDagen
FROM
HuurovereenkomstFiets_LinkTable hf
INNER JOIN
Fiets_LinkTable f
ON hf.HuurovereenkomstFiets_Fiets_id = f.Fiets_id
INNER JOIN
Huurovereenkomst_LinkTable h
ON h.Huurovereenkomst_id = hf.HuurovereenkomstFiets_Huurovereenkomst_id
GROUP BY
f.Fiets_id,
f.Fiets_Type,
YEAR(h.Huurovereenkomst_Begin_datum),
YEAR(h.Huurovereenkomst_Eind_datum)
Local Query
SELECT
t.Fiets_id,
t.Fiets_Type,
t.AantalDagen
FROM
myPassThroughQuery t
WHERE t.BeginYear = [Forms]![FietsAantDagen]![Txtinput]
AND t.EndYear = [Forms]![FietsAantDagen]![Txtinput]
Alternatively, consider using linked tables from SQL Server (with same ODBC connection as pass-through) where Access can treat them as local tables. Hence, you can run your query using form date values. Do note you will have to convert syntax to adhere to Access' SQL dialect like DATEDIFF and JOIN parentheses. Performance may differ due to changed engines but test.
Linked Tables Local Query
SELECT
f.Fiets_id,
f.Fiets_Type,
SUM(DATEDIFF('d', h.Huurovereenkomst_Begin_datum,
h.Huurovereenkomst_Eind_datum)) AS AantalDagen
FROM
(HuurovereenkomstFiets_LinkTable hf
INNER JOIN
Fiets_LinkTable f
ON hf.HuurovereenkomstFiets_Fiets_id = f.Fiets_id)
INNER JOIN
Huurovereenkomst_LinkTable h
ON h.Huurovereenkomst_id = hf.HuurovereenkomstFiets_Huurovereenkomst_id
WHERE YEAR(h.Huurovereenkomst_Begin_datum) = [Forms]![FietsAantDagen]![Txtinput]
AND YEAR(h.Huurovereenkomst_Eind_datum) = [Forms]![FietsAantDagen]![Txtinput]
GROUP BY
f.Fiets_id,
f.Fiets_Type
To create linked tables automatically, use DoCmd.TransferDatabase. Otherwise, use the External Data / ODBC Database icon on ribbon and walk through setup wizard.
DoCmd.TransferDatabase acLink, "ODBC Database", _
"ODBC;DRIVER={SQL Server};Server=<ServerAddress>;Database=<DBname>;Trusted_Connection=Yes;", _
acTable, "HuurovereenkomstFiets", "HuurovereenkomstFiets_LinkedTable"

SQL Select Query via Excel VBA - Defining Parameters, query of a query

I need to place 3 Parameters into a Select query from Excel VBA to SQL - One of which can just be replaced using a variable. But this query is a query of another query, and the parameters are held within those two other queries. If running this query in Access I'm just prompted by all three to manually type them in - "Start", "End", and "AdvisorName".
Running the code will prompt the "No value given for one or more required parameters" - However, only 1 Parameter is in this query, the other 2 parameters are held within the other two queries inside this query - "Q_SoloFocus_Advisor_QuestionsYes" and "Q_SoloFocus_Advisor_QuestionsNo".
The three parameters are called "Start" (Start Date range), "End" (End Date range), and "AdvisorName" (held within this query).
Dim cnn As ADODB.Connection
Dim rst As ADODB.Recordset
Dim Command As New ADODB.Command
Dim strSQL As String
Set cnn = New ADODB.Connection
cnn.Open ConnectionString:=Cnct
Set rst = New ADODB.Recordset
strSQL = "SELECT Q_SoloFocus_Advisor_QuestionsAll.Advisor,
Q_SoloFocus_Advisor_QuestionsAll.KeyID,
Q_SoloFocus_Advisor_QuestionsAll.SubQ_Text,
Q_SoloFocus_Advisor_QuestionsAll.CountOfAnswer AS [All],
Q_SoloFocus_Advisor_QuestionsNo.CountOfAnswer AS [No],
Q_SoloFocus_Advisor_QuestionsYes.CountOfAnswer
AS Yes," & _"Format(Q_SoloFocus_Advisor_QuestionsYes.CountOfAnswer/
Q_SoloFocus_Advisor_QuestionsAll.CountOfAnswer,'0.0%')
AS Result" & _"
FROM (Q_SoloFocus_Advisor_QuestionsAll
LEFT JOIN Q_SoloFocus_Advisor_QuestionsNo
ON (Q_SoloFocus_Advisor_QuestionsAll.SubQ_Text =
Q_SoloFocus_Advisor_QuestionsNo.SubQ_Text)
AND (Q_SoloFocus_Advisor_QuestionsAll.KeyID =
Q_SoloFocus_Advisor_QuestionsNo.KeyID)
AND (Q_SoloFocus_Advisor_QuestionsAll.Advisor =
Q_SoloFocus_Advisor_QuestionsNo.Advisor))
LEFT JOIN Q_SoloFocus_Advisor_QuestionsYes
ON (Q_SoloFocus_Advisor_QuestionsAll.SubQ_Text =
Q_SoloFocus_Advisor_QuestionsYes.SubQ_Text)
AND (Q_SoloFocus_Advisor_QuestionsAll.Advisor =
Q_SoloFocus_Advisor_QuestionsYes.Advisor)
AND (Q_SoloFocus_Advisor_QuestionsAll.KeyID =
Q_SoloFocus_Advisor_QuestionsYes.KeyID)" & _
" WHERE (((Q_SoloFocus_Advisor_QuestionsAll.Advisor)=[AdvisorName]));"
'----------------------------------------------------------------------------------
'----------------------------------------------------------------------------------
rst.Open strSQL, cnn, adOpenStatic
rst.MoveFirst
The SQL for the other two queries are similar, one has a where "No" and the other a where not "No" where the parameters are required are:
SELECT tbl_Surveys.Advisor, tbl_QuestionRef.KeyID, tbl_QuestionRef.CallSkill,
tbl_QuestionRef.CallReason, tbl_QuestionRef.MainQ_Text
tbl_QuestionRef.SubQ_Text,
Count(tbl_SurveyAnswers.Answer) AS CountOfAnswer
FROM tbl_QuestionRef
INNER JOIN (tbl_SurveyAnswers
INNER JOIN tbl_Surveys
ON tbl_SurveyAnswers.SurveyLink = tbl_Surveys.ID)
ON tbl_QuestionRef.KeyID = tbl_SurveyAnswers.QuestionRef
WHERE (((tbl_SurveyAnswers.Answer)<>"No"
And (tbl_SurveyAnswers.Answer)<>"N/A"
And (tbl_SurveyAnswers.Answer)<>"0")
AND ((tbl_Surveys.CallDate)>=[Start]
And (tbl_Surveys.CallDate)<=[End]))
GROUP BY tbl_Surveys.Advisor, tbl_QuestionRef.KeyID, tbl_QuestionRef.CallSkill,
tbl_QuestionRef.CallReason,
tbl_QuestionRef.MainQ_Text, tbl_QuestionRef.SubQ_Text;
As you can see, the "Start" and "End" parameters are in this second SQL query... Any ideas how I can put these two parameters into the first SQL function? I can't put the Start and End into the "All" query, as it'll knock out the "Count" part...
Assuming the other two queries are saved and named, instead of calling named queries, incorporate the SQL from the two queries into the main query as derived tables. By doing so, the query will prompt for all parameters. See the JOIN clauses for some clarification.
strSQL = "SELECT Q_SoloFocus_Advisor_QuestionsAll.Advisor, "
Q_SoloFocus_Advisor_QuestionsAll.KeyID,
Q_SoloFocus_Advisor_QuestionsAll.SubQ_Text,
Q_SoloFocus_Advisor_QuestionsAll.CountOfAnswer AS [All],
Q_SoloFocus_Advisor_QuestionsNo.CountOfAnswer AS [No],
Q_SoloFocus_Advisor_QuestionsYes.CountOfAnswer
AS Yes," & _"Format(Q_SoloFocus_Advisor_QuestionsYes.CountOfAnswer/
Q_SoloFocus_Advisor_QuestionsAll.CountOfAnswer,'0.0%')
AS Result" & _"
FROM (Q_SoloFocus_Advisor_QuestionsAll
LEFT JOIN (YOUR FIRST SAVE QUERY SQL) AS Q_SoloFocus_Advisor_QuestionsNo
ON (Q_SoloFocus_Advisor_QuestionsAll.SubQ_Text =
Q_SoloFocus_Advisor_QuestionsNo.SubQ_Text)
AND (Q_SoloFocus_Advisor_QuestionsAll.KeyID =
Q_SoloFocus_Advisor_QuestionsNo.KeyID)
AND (Q_SoloFocus_Advisor_QuestionsAll.Advisor =
Q_SoloFocus_Advisor_QuestionsNo.Advisor))
LEFT JOIN (YOUR SECOND QUWERY SQL) AS Q_SoloFocus_Advisor_QuestionsYes
ON (Q_SoloFocus_Advisor_QuestionsAll.SubQ_Text =
Q_SoloFocus_Advisor_QuestionsYes.SubQ_Text)
AND (Q_SoloFocus_Advisor_QuestionsAll.Advisor =
Q_SoloFocus_Advisor_QuestionsYes.Advisor)
AND (Q_SoloFocus_Advisor_QuestionsAll.KeyID =
Q_SoloFocus_Advisor_QuestionsYes.KeyID)" & _
" WHERE (((Q_SoloFocus_Advisor_QuestionsAll.Advisor)=[AdvisorName]));"

Another Syntax error (missing operator) in query expression

I need a little help developing my SQL query. My goal is to remove an entry. My condition is two tables away. I've gotten this far but I can't seem to find where the other mistake
con.Execute "DELETE FROM Expenses INNER JOIN Agreements ON Agreements.AgreementsID =
Expenses.AgreementID AND INNER JOIN Audits
ON Audits.AuditID = Agreements.AuditID WHERE Audits.Share = True"
I'm using access 2007 and my con variable is
con.Open _
"Driver={Microsoft Access Driver (*.mdb, *.accdb)};" & _
"Dbq=" & Loc & ";"
Try This, You are using AND before INNER JOIN, remove that only.
DELETE DISTINCTROW Expenses.*
FROM Expenses
INNER JOIN Agreements ON Agreements.AgreementsID = Expenses.AgreementID
INNER JOIN Audits ON Audits.AuditID = Agreements.AuditID
WHERE Audits.Share = True
[EDIT Query]
DELETE Expenses.*
FROM Expenses
WHERE Expenses.AgreementID IN (
SELECT Agreements.AgreementID FROM Agreements
INNER JOIN Audits ON Audits.AuditID = Agreements.AuditID
WHERE Audits.Share = True
)
OP get error:- Too few parameters Expected 2.
To handle this, This error may be obtained because the any column names being selected have special characters in it. If there are special characters in the column names of the database, the name should be surrounded with brackets in the SQL query. So if you have any column name which have special char value , then try [ColumnName]