tom_optimum
asked on
MS Access Query Build
Hi all,
I have a query in MS Access. It holds two fields:
I want a third field that calculates a value based on the two fields above. I need it to do it based on this method:
If the NCyearActual is 'N1' and the StartDate is between 1st September to 31st December (in this calendar year) then show 'Nursery 2' - else, show the value held in the NCyearActual field.
I've tried a few things but I can't get it to work.
Can someone help me with this one please?
Your help would be much appreciated.
Thanks,
Tom
I have a query in MS Access. It holds two fields:
StartDate
NCyearActual
I want a third field that calculates a value based on the two fields above. I need it to do it based on this method:
If the NCyearActual is 'N1' and the StartDate is between 1st September to 31st December (in this calendar year) then show 'Nursery 2' - else, show the value held in the NCyearActual field.
I've tried a few things but I can't get it to work.
Can someone help me with this one please?
Your help would be much appreciated.
Thanks,
Tom
Just add a new column with something like this:
iif ( N1=1, iif ( StartDate BETWEEN #09-01-2014# AND #12-31-2014#, 'Nursery 2', NCyearActual), NCyearActual)
For the current year you need to build the date on runtime:
iif ( N1=1, iif ( StartDate BETWEEN DateValue("09-01-" + Str(YEAR(Date()))) AND DateValue("12-31-" + Str(YEAR(Date()))), 'Nursery 2', NCyearActual), NCyearActual)
ASKER
Great - thanks. I'm just about to go into a meeting. I will try these when I am out.
I do need it to work with current calendar year because I don't want to have to update the code each year.
I do need it to work with current calendar year because I don't want to have to update the code each year.
Never use string handling for dates:
SELECT
StartDate,
NCyearActual,
IIf(N1 = 1,
IIf(StartDate BETWEEN DateSerial(Year(Date()), 9, 1) AND DateSerial(Year(Date()), 12, 31)),
'Nursery 2', NCyearActual), NCyearActual) AS NC
FROM
tblYourTable
/gustav
SELECT
StartDate,
NCyearActual,
IIf(N1 = 1,
IIf(StartDate BETWEEN DateSerial(Year(Date()), 9, 1) AND DateSerial(Year(Date()), 12, 31)),
'Nursery 2', NCyearActual), NCyearActual) AS NC
FROM
tblYourTable
/gustav
ASKER
Hi all,
None of these work.. I get some form of error for them all.
For:
I get this error:
For:
And for this one:
I get this:
Also, I think the code saying things like "N1 = 1" is wrong... I dont have a field called N1 my field is called NCyearActual and one of the values is 'N1'.
Does this help?
Cheers
Tom
None of these work.. I get some form of error for them all.
For:
iif ( N1=1, iif ( StartDate BETWEEN #09-01-2014# AND #12-31-2014#, 'Nursery 2', NCyearActual), NCyearActual)
I get this error:
For:
iif ( N1=1, iif ( StartDate BETWEEN DateValue("09-01-" + Str(YEAR(Date()))) AND DateValue("12-31-" + Str(YEAR(Date()))), 'Nursery 2', NCyearActual), NCyearActual)
I get this:And for this one:
SELECT
StartDate,
NCyearActual,
IIf(N1 = 1,
IIf(StartDate BETWEEN DateSerial(Year(Date()), 9, 1) AND DateSerial(Year(Date()), 12, 31)),
'Nursery 2', NCyearActual), NCyearActual) AS NC
FROM
tblYourTable
I get this:
Also, I think the code saying things like "N1 = 1" is wrong... I dont have a field called N1 my field is called NCyearActual and one of the values is 'N1'.
Does this help?
Cheers
Tom
This should do:
SELECT
StartDate,
NCyearActual,
IIf(NCyearActual = 'N1',
IIf(StartDate BETWEEN DateSerial(Year(Date()), 9, 1) AND DateSerial(Year(Date()), 12, 31),
'Nursery 2', NCyearActual), NCyearActual) AS NC
FROM
tblYourTable
/gustav
SELECT
StartDate,
NCyearActual,
IIf(NCyearActual = 'N1',
IIf(StartDate BETWEEN DateSerial(Year(Date()), 9, 1) AND DateSerial(Year(Date()), 12, 31),
'Nursery 2', NCyearActual), NCyearActual) AS NC
FROM
tblYourTable
/gustav
ASKER
Try this:
SELECT
StartDate,
NCyearActual,
IIf(NCyearActual = 'N1' And (StartDate BETWEEN DateSerial(Year(Date()), 9, 1) AND DateSerial(Year(Date()), 12, 31)), 'Nursery 2', NCyearActual) AS NC
FROM
BasicPupilData
/gustav
SELECT
StartDate,
NCyearActual,
IIf(NCyearActual = 'N1' And (StartDate BETWEEN DateSerial(Year(Date()), 9, 1) AND DateSerial(Year(Date()), 12, 31)), 'Nursery 2', NCyearActual) AS NC
FROM
BasicPupilData
/gustav
But there is no subquery. Could you paste your full SQL please?
/gustav
/gustav
ASKER
Hi,
Here it is:
Here it is:
SELECT CTFImport.UPN, CTFImport.Surname, CTFImport.Forename, CTFImport.DOB, Get_FSM_Calc.FSM AS FSM, YearGroupLookUp.OTrackYearGroup AS NCyearActual, CTFImport.Gender, CTFImport.Ethnicity, EALLookUp.OTrack_EAL, Max(CTFImport.StartDate) AS MaxOfStartDate, Get_InCare_Calc.InCareStatus
FROM (((CTFImport LEFT JOIN EALLookUp ON CTFImport.EnglishAsAdditionalLanguage = EALLookUp.CTF_EAL) LEFT JOIN Get_FSM_Calc ON CTFImport.UPN = Get_FSM_Calc.UPN) LEFT JOIN Get_InCare_Calc ON CTFImport.UPN = Get_InCare_Calc.UPN) LEFT JOIN YearGroupLookUp ON CTFImport.NCyearActual = YearGroupLookUp.CTFYearGroup
GROUP BY CTFImport.UPN, CTFImport.Surname, CTFImport.Forename, CTFImport.DOB, Get_FSM_Calc.FSM, YearGroupLookUp.OTrackYearGroup, CTFImport.Gender, CTFImport.Ethnicity, EALLookUp.OTrack_EAL, Get_InCare_Calc.InCareStatus, CTFImport.EnglishAsAdditionalLanguage
ORDER BY YearGroupLookUp.OTrackYearGroup;
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Thanks foyour help on this - very impressed.
Thanks
Tom
Thanks
Tom
You are welcome!
/gustav
/gustav
Open in new window