I have a report whereby I create the RecordSource in the report's module based on selection criteria. The sql threw an error. I broke out the SQL statement and tried to isolate it in a test subroutine.
The SQL statement inserts two variables which I've determined are there by using the Debug.Print statement.
When I copy the SQL statement and put it into a query, removing the quotations and & _ characters at the ends of lines, and insert the date into the SQL as well as a number for the other variable, the query brings back the data I ask for.
But the SQL in the module throws a runtime error 3141, "The SELECT statement includes a reserved word or an argument name that is
Bear in mind in order to see all the code in the code window I had to truncate lines with " & _ on the fly. I'm confident that the SQL statement in the module is proper.
I'm scratching whats left of my head hair not being able to understand why it's throwing the error.
Remember when I put that SQL into a query and replace the Edat with a hashtag bracketed date, and replace compy with a number, the query works.
Any insights will be welcome.
Thanks,
Vic
The SQL statement inserts two variables which I've determined are there by using the Debug.Print statement.
When I copy the SQL statement and put it into a query, removing the quotations and & _ characters at the ends of lines, and insert the date into the SQL as well as a number for the other variable, the query brings back the data I ask for.
But the SQL in the module throws a runtime error 3141, "The SELECT statement includes a reserved word or an argument name that is
misspelled or missing, or the punctuation is incorrrect."
Code:
Sub testSQL()
Dim compy As Integer
Dim Edat As Date
Dim sql As String
compy = 4
Edat = #11/6/2017#
Debug.Print compy & ", " & Edat
sql = "SELECT (tblMembers.MemFName & ' ' & tblMembers.MemLName) AS MemName, " & _
"tblMembers.MemAddress1,tblMembers.MemAddress2, tblMembers.MemCity, tblMembers.MemST, " & _
"tblMembers.MemZipcode, tblAltEmpInfo.fldAltAddr1, tblAltEmpInfo.fldAltAddr2, " & _
"tblAltEmpInfo.fldAltCity, tblAltEmpInfo.fldAltState, " & _
"tblAltEmpInfo.fldAltZip, IIf(Len(tblAltEmpInfo.fldAltAddr1)>0,tblAltEmpInfo.fldAltZip," & _
"tblMembers.MemZipcode) AS TruZip" & _
"FROM tblMembers INNER JOIN tblAltEmpInfo ON tblMembers.MemPRIID = tblAltEmpInfo.PriID" & _
"WHERE (((tblMembers.Mem_UnitNo)<>999) AND ((tblMembers.MemStatusID)=17 Or " & _
"(tblMembers.MemStatusID)=2)) AND ((tblMembers.MemMemberTypeID)=3 Or " & _
"(tblMembers.MemMemberTypeID)=6) AND ((tblMembers.MemClassID)<>8 And " & _
"(tblMembers.MemClassID)<>14 And (tblMembers.MemClassID)<>15) AND ((tblMembers.MemEmp)=" & compy & _
") AND ((tblMembers.HideRec)=False) " & _
"OR (((tblMembers.Mem_UnitNo)<>999) AND ((tblMembers.MemStatusID)=20 Or " & _
"(tblMembers.MemStatusID)=21) AND ((tblMembers.MemMemberTypeID)=3 Or " & _
"(tblMembers.MemMemberTypeID)=6) AND ((tblMembers.MemClassID)<>8 And " & _
"(tblMembers.MemClassID)<>14 And (tblMembers.MemClassID)<>15) " & _
"AND ((tblMembers.MemEffective)>=#" & Edat & "#-90) AND ((tblMembers.MemEmp)=" & compy & _
") AND ((tblMembers.HideRec)=False))" & _
"ORDER BY IIf(Len(tblAltEmpInfo.fldAltAddr1)>0,tblAltEmpInfo.fldAltZip,tblMembers.MemZipcode), " & _
"tblMembers.MemLName, tblMembers.Mem_UnitNo, tblMembers.MemFName;"
DoCmd.RunSQL sql
End Sub
Bear in mind in order to see all the code in the code window I had to truncate lines with " & _ on the fly. I'm confident that the SQL statement in the module is proper.
I'm scratching whats left of my head hair not being able to understand why it's throwing the error.
Remember when I put that SQL into a query and replace the Edat with a hashtag bracketed date, and replace compy with a number, the query works.
Any insights will be welcome.
Thanks,
Vic