I have a query in Access which is returning data from two tables. The one table needs to be filtered however as I need to return the most recent entry for a particular date.
I have created the query by copying the SQL from another query and then modifying it in the design view. When I save the query from design view and run it I get the expected outcome of a three column table with totals for each ShiftType for each StaffNumber. However when I move the query over to my VBA application it throws an error. Also when I view the query in SQL view and modify it (add and then remove whitespace) it throws an error.
The query SQL is:
SELECT DataLeave.StaffNumber, TABLE1.ShiftType, Count(TABLE1.ShiftType) AS CountOfShiftType
FROM DataLeave INNER JOIN [SELECT DataShifts.StaffNumber, ShiftType, DataShifts.ShiftDate
FROM DataShifts
INNER JOIN [SELECT StaffNumber, ShiftDate, MAX(ID) AS IDMAX FROM DataShifts GROUP BY DataShifts.StaffNumber,DataShifts.ShiftDate]. AS FILTER ON DataShifts.ID = FILTER.IDMAX]. AS TABLE1 ON (DataLeave.LeaveDate = TABLE1.ShiftDate) AND (DataLeave.StaffNumber = TABLE1.StaffNumber)
WHERE (((DataLeave.Active)=True) AND ((DataLeave.LeaveDate) Between #9/3/2013# And #3/15/2014#) AND ((DataLeave.StaffNumber)='2537860')) OR (((DataLeave.StaffNumber)='2524710')) OR (((DataLeave.StaffNumber)='2515610') AND ((DataLeave.LeaveTypeShort) Like '*FD'))
GROUP BY DataLeave.StaffNumber, TABLE1.ShiftType
ORDER BY DataLeave.StaffNumber;
The error that I get is:
Syntax error in query experssion 'DataShifts.ID = FILTER.IDMAX]. AS TABLE1 ON (DataLeave.LeaveDate = TABLE1.ShiftDate)'
I have designed another query which works using IN but it is extremely slow so I would like to get this working but no amount of tweaking so far has worked.
The VBA code is:
q = "SELECT DataLeave.StaffNumber, TABLE1.ShiftType, Count(TABLE1.ShiftType) AS CountOfShiftType "
q = q + "FROM DataLeave INNER JOIN [SELECT DataShifts.StaffNumber, ShiftType, DataShifts.ShiftDate "
q = q + "FROM DataShifts "
q = q + "INNER JOIN [SELECT StaffNumber, ShiftDate, MAX(ID) AS IDMAX FROM DataShifts GROUP BY DataShifts.StaffNumber,DataShifts.ShiftDate]. AS FILTER ON DataShifts.ID = FILTER.IDMAX]. AS TABLE1 ON (DataLeave.LeaveDate = TABLE1.ShiftDate) AND (DataLeave.StaffNumber = TABLE1.StaffNumber) "
q = q + "WHERE (((DataLeave.Active)=True) AND ((DataLeave.LeaveDate) Between #9/3/2013# And #3/15/2014#) AND ((DataLeave.StaffNumber)='2537860')) OR (((DataLeave.StaffNumber)='2524710')) OR (((DataLeave.StaffNumber)='2515610') AND ((DataLeave.LeaveTypeShort) Like '*FD')) "
q = q + "GROUP BY DataLeave.StaffNumber, TABLE1.ShiftType "
q = q + "ORDER BY DataLeave.StaffNumber;"
Set Rs = Db.OpenRecordset(q)
using DAO