I'm trying to figure out why I'm getting this error. I'm relatively inexperienced. The nature of the following code is that a slightly different query is run whether or not the "ImportantDates Only" flag is true or not. The problem is that the query that runs while the flag is true fails, and the query for when the flag is false does not fail. The error occurs at db.OpenRecordse t (last line in the code segment). The failing query does run fine when cut and paste directly into a new query and then run. I've underlined the sections that are different below. This is in Access 2007.
Code Segment
Calculated Query that Works
Calculated Query that Doesn't Work
Error Received for Second Query
Code Segment
Code:
Dim db As DAO.Database
Dim rs As DAO.Recordset
Dim strSQL As String
Set db = OpenDatabase(systemDbPath & "Data.accdb")
If ImportantDatesOnly = True Then
strSQL = "SELECT [etl].[TaskID], [qec].[EventID], [qec].[InstanceID], " & _
"IIf([tblEventException].[EventID] Is Null,IIf(([qec].[PeriodTypeID] Is Null) Or " & _
"([qec].[PeriodFreq] Is Null) Or ([qec].[InstanceID] Is Null), " & _
"[qec].[EventStart],DateAdd([qec].[PeriodTypeID], " & _
"[qec].[InstanceID]*[qec].[PeriodFreq],[qec].[EventStart])), " & _
"IIf([tblEventException].[IsCanned],Null,[tblEventException].[InstanceDate])) AS EventDate, " & _
"[qec].[EventDescrip], [qec].[Comment], [tblEventException].[IsCanned], " & _
"[tblEventException].[InstanceComment], [qec].[EventStart], [qec].[RecurCount], " & _
"[qec].[PeriodFreq], [ltPeriodType].[PeriodType] FROM ((qryEventCartesian As qec LEFT JOIN " & _
"tblEventException ON ([qec].[InstanceID] = [tblEventException].[InstanceID]) AND " & _
"([qec].[EventID] = [tblEventException].[EventID])) LEFT JOIN ltPeriodType ON " & _
"[qec].[PeriodTypeID] = [ltPeriodType].[PeriodTypeId]) RIGHT JOIN EventsTasksLink AS etl ON " & _
"[qec].[EventID] = [etl].[EventID] WHERE ((([etl].[TaskID]) = " & TaskID [I][U]& ") And " & _
"(([qec].[IsImportant]) = True)[/U][/I]) ORDER BY [qec].[EventID], [qec].[InstanceID];"
Else
strSQL = "SELECT [etl].[TaskID], [qec].[EventID], [qec].[InstanceID], " & _
"IIf([tblEventException].[EventID] Is Null,IIf(([qec].[PeriodTypeID] Is Null) " & _
"Or ([qec].[PeriodFreq] Is Null) Or ([qec].[InstanceID] Is Null)," & _
"[qec].[EventStart],DateAdd([qec].[PeriodTypeID]," & _
"[qec].[InstanceID]*[qec].[PeriodFreq],[qec].[EventStart]))," & _
"IIf([tblEventException].[IsCanned],Null,[tblEventException].[InstanceDate])) AS EventDate, " & _
"[qec].[EventDescrip], [qec].[Comment], [tblEventException].[IsCanned], " & _
"[tblEventException].[InstanceComment], [qec].[EventStart], [qec].[RecurCount], " & _
"[qec].[PeriodFreq], [ltPeriodType].[PeriodType] FROM ((qryEventCartesian AS qec LEFT JOIN " & _
"tblEventException ON ([qec].[InstanceID] = [tblEventException].[InstanceID]) AND " & _
"([qec].[EventID] = [tblEventException].[EventID])) LEFT JOIN ltPeriodType ON " & _
"[qec].[PeriodTypeID] = [ltPeriodType].[PeriodTypeId]) RIGHT JOIN EventsTasksLink AS etl ON " & _
"[qec].[EventID] = [etl].[EventID] WHERE [etl].[TaskID] = " & TaskID & _
" ORDER BY [qec].[EventID], [qec].[InstanceID];"
End If
Set rs = db.OpenRecordset(strSQL)
Code:
SELECT [etl].[TaskID],[qec].[EventID],[qec].[InstanceID], IIf([tblEventException].[EventID] Is Null,IIf(([qec].[PeriodTypeID] Is Null) Or ([qec].[PeriodFreq] Is Null) Or ([qec].[InstanceID] Is Null), [qec].[EventStart],DateAdd([qec].[PeriodTypeID],[qec].[InstanceID]*[qec].[PeriodFreq],[qec].[EventStart])), IIf([tblEventException].[IsCanned],Null,[tblEventException].[InstanceDate])) AS EventDate, [qec].[EventDescrip], [qec].[Comment], [tblEventException].[IsCanned],[tblEventException].[InstanceComment], [qec].[EventStart],[qec].[RecurCount],[qec].[PeriodFreq],[ltPeriodType].[PeriodType] FROM ((qryEventCartesian As qec LEFT JOIN tblEventException ON ([qec].[InstanceID] = [tblEventException].[InstanceID]) AND ([qec].[EventID] = [tblEventException].[EventID])) LEFT JOIN ltPeriodType ON [qec].[PeriodTypeID] = [ltPeriodType].[PeriodTypeId]) RIGHT JOIN EventsTasksLink AS etl ON [qec].[EventID] = [etl].[EventID] WHERE (([etl].[TaskID]) = 8) ORDER BY [qec].[EventID], [qec].[InstanceID];
Code:
SELECT [etl].[TaskID], [qec].[EventID], [qec].[InstanceID], IIf([tblEventException].[EventID] Is Null,IIf(([qec].[PeriodTypeID] Is Null) Or ([qec].[PeriodFreq] Is Null) Or ([qec].[InstanceID] Is Null), [qec].[EventStart],DateAdd([qec].[PeriodTypeID], [qec].[InstanceID]*[qec].[PeriodFreq],[qec].[EventStart])), IIf([tblEventException].[IsCanned],Null,[tblEventException].[InstanceDate])) AS EventDate, [qec].[EventDescrip], [qec].[Comment], [tblEventException].[IsCanned], [tblEventException].[InstanceComment], [qec].[EventStart], [qec].[RecurCount], [qec].[PeriodFreq], [ltPeriodType].[PeriodType] FROM ((qryEventCartesian As qec LEFT JOIN tblEventException ON ([qec].[InstanceID] = [tblEventException].[InstanceID]) AND ([qec].[EventID] = [tblEventException].[EventID])) LEFT JOIN ltPeriodType ON [qec].[PeriodTypeID] = [ltPeriodType].[PeriodTypeId]) RIGHT JOIN EventsTasksLink AS etl ON [qec].[EventID] = [etl].[EventID] WHERE ((([etl].[TaskID]) = 8[U][I]) And (([qec].[IsImportant]) = True)[/I][/U]) ORDER BY [qec].[EventID], [qec].[InstanceID];
Code:
Run-time error '3061' Too few parameters. Expected 1.
Comment