Dsum Function in Query Field not working properly - ms-access

I have a date based query that returns two fields, Week Ending Date and L2N, and I want to add a third field that provides a rolling total of the L2N field. I have tried using DSum as follows:
RunL2N: DSum("[L2N]","Occupied Apts & L2N", "Week Ending Date=" & "'" & [Week Ending Date] & "'")
In the code above, L2N is the field I want to sum, and Occupied Apts & L2N is the query that returns the fields. The query asks for a Week Ending Date and then delivers all of the records that equal or proceed the given Week Ending Date.
It no worky right. My goal is for the RunL2N field to show a rolling total of the L2N field for each record. In other words, if the query returns multiple records, I want it to show the L2N field result, and then show the Run2L2N field, which sums the L2N fields of the records above and the current record.
So if the query returns a 1 in the L2N field, then a 3 for the next record, then a 5 for the next record and lastly a 7 for the final record, I want the RunL2N field to show 1 for the first record, 4 for the next record, 9 for the next record and lastly 16 for the final record.

Since the field name includes spaces, bracket it like this: [Week Ending Date]
Assuming it's Date/Time type, use # delimiters before and after the date value.
Finally I think you want to get the sum from rows where [Week Ending Date] <= the date in the current row.
DSum("[L2N]","Occupied Apts & L2N", "[Week Ending Date]<=" & Format([Week Ending Date], "\#yyyy-m-d\#"))
However if you use a correlated subquery, instead of DSum(), to compute the running sum of L2N, you won't have to bother about delimiters for the date value.
SELECT
y1.[Week Ending Date],
y1.L2N,
(
SELECT Sum(y2.L2N)
FROM [Occupied Apts & L2N] AS y2
WHERE y2.[Week Ending Date] <= y1.[Week Ending Date]
) AS RunL2n
FROM [Occupied Apts & L2N] AS y1;

Related

Update Query - update to day before next date

I am trying to create a query which will update a blank "To Date" field with the day prior to next updated date.
Example, an [Item Number] standard cost was updated ([From Date]) on 01/03/2019, then again on 01/07/2019 and then again on the 01/01/2020.
I would like an adjacent column which is updated with [To Date] of 30/06/2019, 31/12/2019.
I will run a subsequent query which updates blanks (i.e. current cost as there is no next [From Date]) to Today End of Month date (I assume I need a separate query for this rather than an IIF which can populate Blanks as part of this update query?)
Currently I have below, but it is updating the [To Date] with day prior to the newest date in all instances (i.e 31/12/2019 for first 2 rows), I understand that I need a SORT within the below query:
Many thanks in advance from this first time poster!
UPDATE
Standards
INNER JOIN
Standards AS Standards_1
ON
(Standards.[Item number] = Standards_1.[Item number]) AND (Standards.[From date] < Standards_1.[From date])
SET
Standards.[To Date 2] = Standards_1.[From date]-1;
This might work, but the performance migth be a bit slow:
UPDATE Standards
SET [To Date] = Nz(
DateAdd("d", -1,
DMin("[From Date]", "Standards", "[Item Number]=" & [Item Number] & " AND [From Date] > #" & [From Date] & "#")
)
, DateSerial(Year(Date()), Month(Date())+1, 0)
)
Start from the DMin function, which will lookup the next date greater than the current date for the item.
DateAdd function will subtract one day from this date.
Nz will use the value returned by the DateSerial function if the DateAdd function result is Null.
DateSerial function returns the end date of the current month.
# are added because of the date value of the [From Date] field in the criteria of the DMin.
Instead of using the [From Date] field in the DMin function criteria, you can consider using the Autonumber Primary Key field. You'll need to remove the # in the criteria.
Everything between Nz(...) will need to be on one line.
This solves both of your requirements, just test the performance.

Error when runing query from form between two date from a form

I have a form called FirstInLastOut which looks as the image below.
Based on Name or badge number I want to search between two dates.
I am using the following criteria on the query:
>=[Forms]![FirstInLastOut]![StartDateEntry] And <=[Forms]![FirstInLastOut]![EndDateEntry]
This is given me results that include other months as well. Please see the query report below.
So as you can see in the image the numbers of the dates are falling with the the parameter but getting other months as well.
How can I make it so it will only select the dates between the date ranges?
SELECT FistClockInRaw.Badgenumber, FistClockInRaw.name, FistClockInRaw.lastname, FistClockInRaw.MinOfCHECKTIME, FLastClockOutRaw.MaxOfCHECKTIME, [MaxOfCHECKTIME]-[MinOfCHECKTIME] AS TotalHours, FLastClockOutRaw.QDate, FistClockInRaw.MinOfQTime, FLastClockOutRaw.MaxOfQTime, RawCompleteQuery.CHECKTIME
FROM RawCompleteQuery, FLastClockOutRaw INNER JOIN FistClockInRaw ON (FLastClockOutRaw.Badgenumber = FistClockInRaw.Badgenumber) AND (FLastClockOutRaw.name = FistClockInRaw.name) AND (FLastClockOutRaw.QDate = FistClockInRaw.QDate)
WHERE (((FistClockInRaw.name)=[Forms]![FirstInLastOut]![FirstNameEntry]) AND ((RawCompleteQuery.CHECKTIME)>=[Forms]![FirstInLastOut]![StartDateEntry] And (RawCompleteQuery.CHECKTIME)<=[Forms]![FirstInLastOut]![EndDateEntry]));
is the Query
I assume that the forms fields StartDateEntry and EndDateEntry are bound to fields of type date.
I also assume that you are only interested to compare the date part of those form fields.
So try this condition instead to assure correct date interpreting:
WHERE FistClockInRaw.name=[Forms]![FirstInLastOut]![FirstNameEntry]
AND RawCompleteQuery.CHECKTIME >= Format([Forms]![FirstInLastOut]![StartDateEntry], "\#yyyy-mm-dd\#")
AND RawCompleteQuery.CHECKTIME <= Format([Forms]![FirstInLastOut]![EndDateEntry], "\#yyyy-mm-dd\#")
A remark:
Be aware that every date field/variable always contains a time part too!
So your current logic comparing EndDateEntry with <= can cause trouble, because you would only get results of the end date having time values of 00:00:00 in the field CHECKTIME.
If any record of CHECKTIME contains the requested end date and a time part bigger then 00:00:00, it is not in the result.
To avoid that, you should use < and add one day:
And RawCompleteQuery.CHECKTIME < Format([Forms]![FirstInLastOut]![EndDateEntry] + 1, "\#yyyy-mm-dd\#")

MS Access DateDiff Ambiguous Dates (Day Month order) Returning bad results

tblDurations
TaskID : Number
Assigned Date : Date/Time
Start Date : Date/Time
End Date : Date/Time
SELECT TaskID,
Count(TaskID) As [Task Count],
Min(Nz([Start Date], [Assigned Date])) as [Min of Start Date],
Max([End Date]) as [Max of End Date],
DateDiff("d", Min(Nz([Start Date], [Assigned Date])), Max([End Date])) + 1 as [Date Range]
FROM tblDurations
GROUP BY TaskID
Output:
TaskID Task Count Min of Start Date Max of End Date Date Range
1 3 16/08/2018 10/01/2019 1
2 2 4/09/2017 07/09/2017 4
3 3 13/09/2017 08/01/2018 118
I am getting the occasional row as shown in row 1 (TaskID = 1) which should equal 148!
After a lot of checking I determined this occurs for dates that are ambiguous. By that I mean where the day and months values can be confused for each other.
I'm in Australia where the date formnat is dd/mm/yyyy. However I believe the datediff function is confusing the day and month order and returning Zero (plus my 1 = 1).
How can I overcome this issue?
The problem was not the ambiguous dates. Looking closer I found various non ambiguous dates returning bad values.
The problem was the use of an Nz function which I was using like this :
Min(NZ([Start Date], [Assigned Date]))
Which I took to mean take the Min of either the Start Date, but if the start date does is NULL then the min of the Assigned Date.
What does work is this :
Nz(Min([StartDate], Min(Assigned Date))
All fixed.
Which begs the question. Why only sometimes? As it was not related to Start Dates being NULL or not NULL.
Thanks everyone for feed back!

IsNull function inside an IIf() not working - MS Access

I have a field in a query that is checking how the user entered the date on the main form. I am trying to make it like a single entered date when the first text box is fill in and like a date range if an ending date is entered into the second text box. So if the ending date text box is blank, it should be ignored and the query should run as if filtering only on a specific date. I used the below expression and only entered the starting date, so it should only consider the field as being filtered by 1 date, not a range. But the query returns blank. [Text0] is the starting date and [Text3] is the ending date. The field is a job date field intended to only return job numbers from either that date or inside the date range.
Example: If [Text0] is set to 4/20/2015 and [Text3] is blank, the query should return job numbers A-18, B-18, and C-18. If [Text0] is set to 4/20/2015 and [Text3] is set to 4/27/2015, the query should return A-18, B-18, C-18, D-19, E-19, F-19. The difference between -18 and -19 is the week that it corresponds to.
=IIf((IsNull([Forms]![MainForm]![Text3])=True),[Forms]![MainForm]![Text0],Between [Forms]![MainForm]![Text0] And [Forms]![MainForm]![Text3])
I think what you need is something like this:
Select JobNumber, ...
from Jobs
where JobDate >= [Forms]![MainForm]![Text0]
and JobDate <= nz([Forms]![MainForm]![Text3],[Forms]![MainForm]![Text0])

Use the Date as a criteria in Dsum function, access

I have to generate a rolling sum or Cumulative sum of the OIL field for each WELLNMBR as per its MONTH value.
The query used is
{SELECT tblProductionData.WELLNMBR, tblProductionData.[Month], tblProductionData.DAYS, tblProductionData.OIL, tblProductionData.GAS, tblProductionData.WATER, DSum("[OIL]","tblProductionData","WELLNMBR=" & "'" & [WELLNMBR] & "'" & "AND MONTH <=" & "#" & [MONTH] & "#") AS Expr2
FROM tblProductionData;
In the result the sum changes after every year, not after a month. I would like to know the reason for this strange behavior and the method to obtain the rolling sum in every month.
Incorrect result
http://i.stack.imgur.com/NFBRm.png
Desired result
http://i.stack.imgur.com/U8I9r.png
Your query begins '{'. This needs to be removed. Also 'MONTH' is a reserved word. Try replacing it with 'Mth', then proceed from there.