I have a query (in MS Access 2013) that gives me the Sales for various Items by Date, for each day for the next 12mo. In another table, I have each Item with its known purchasing leadtime, say between 30 and 90 days.
I created a query, and for each Item, I calculated the future date according to the leadtime, using:
FutureDate: DateAdd("d",[Leadtime],Date())
I validated all Items exist in the Sales query, and all FutureDates are within the records that exist in Sales.
I need to calculate the sum of daily Sales between now and the calculated [FutureDate] for each Item, to get the total amount of Sales expected within the unique Leadtime of each item.
I tried function DSUM() with weird results:
The query of daily Sales already excludes any past sales, so my first try was:
TotalSalesInLeadtime: DSUM("DailySales","Sales","[DayOfSale]<=#" & [FutureDate] & "# AND [Item]='" & [SearchedItem] &"'")
For some Items, [TotalSalesInLeadtime] calculated correctly, while others evaluated to Null.
Then I tried:
TotalSalesInLeadtime: DSUM("DailySales","Sales","[DayOfSale] BETWEEN #" Date() "# AND #" & [FutureDate] & " AND [Item]='" & [SearchedItem] &"'")
The results now were reversed. [TotalSalesInLeadtime] values now showed correctly for the items that previously showed Null, and were Null for items that previously evaluated correctly.
I never figured out why DSUM() did this.
To work around the DSUM() glitch, I went with an embedded subquery, which yielded all the values correctly, albeit at a significant performance hit:
SELECT [PurchItem],
(SELECT Sum([DailySales]) AS SumOfSales
FROM [Sales]
WHERE ([Item]=[LeadtimeItems].[PurchItem]) AND ([DayOfSale] Between Date() AND [LeadtimeItems].[FutureDate]))
As TotalSalesInLeadtime
FROM LeadtimeItems
If anyone has a clue why DSUM may behave this way, I'd appreciate the help. DSUM, when it works, certainly seems to go faster.
When "gluing together" SQL statements (or fragments) that include date literals enclosed in hash marks (#), one must bear in mind that Access SQL and VBA will always interpret ambiguous date literals as mm-dd-yyyy regardless of the system-wide date format. So on a machine where Windows has been configured to use dd-mm-yyyy, an unambiguous date like April 30 will work okay
?DateSerial(2013,4,30)
30-04-2013
?"#" & DateSerial(2013,4,30) & "#"
#30-04-2013#
?Eval("#" & DateSerial(2013,4,30) & "#")
30-04-2013
...but for the next day, May 1, things don't work so well
?DateSerial(2013,5,1)
01-05-2013
?"#" & DateSerial(2013,5,1) & "#"
#01-05-2013#
?Eval("#" & DateSerial(2013,5,1) & "#")
05-01-2013
So the lesson is that any time we "glue together" date literals we must ensure that those dates are in an unambiguous format like yyyy-mm-dd. With regard to this particular question, we need to use
TotalSalesInLeadtime: DSUM("DailySales","Sales","[DayOfSale]<=#" & Format([FutureDate], "yyyy-mm-dd") & "# AND [Item]='" & [SearchedItem] &"'")
Related
I have the below query which is meant to only return lines that are less than the ETA Date.
When applying the criteria below it does return the lines in which the ETA Date is less than today, however it does return the other lines as well and i don't want that.
I just want it to return the lines in which dates are lower than today's date.
Overdue: IIf([PO ETA]<=Now(),DateDiff("yyyy",[PO ETA],Now()) & "year(s)" & DateDiff("m",[PO ETA],Now()) & "month(s)" & DateDiff("d",[PO ETA],Now()) & "day(s)")
I have tried to add something for the else statement but i can't seem to figure out what i should add.
You miss the criteria for that Overdue expression:
<>""
I think this should be simple, but I've been struggling for too long.
I have a form with a bound text box which I would like to return a value it has calculated. It calls on the table 'Training' which stores the variables Client, Standard and Date. Client is a string, standard is an integer and Date is in the format dd/mm/yyyy. I would like the box to return the sum of the Standard entries for this year.
So, if we've been out to see Client A last week and done one session and been out again today and done two, the table might look like:
Client A - 1 - 01/01/2017 <br/>
Client A - 1 - 12/01/2018 <br/>
Client A - 2 - 17/01/2018 <br/>
Client B - 1 - 15/01/2018
I'd like my box to return the value '3', ignoring Client A's training last year or the training this year of any other client.
I can get the box to correctly distinguish between clients, but not between years using this code:
=DSum("[Standard]","Training","[Client] = '" & [Client] & "'")
That selects the client from the training table, based on the client record currently selected in the form. It sums all the training that client has ever had - so in the example above returns '4'.
What I am struggling with is how to restrict the year. I've tried a number of variations on:
& "Year([TrainingDate])=#" & Year(Date())
but always get either Name? or Error. What am I doing wrong?
With thanks,
Matt
Use:
=DSum("[Standard]","Training","[Client] = '" & [Client] & "' And Year([TrainingDate]) = Year(Date())")
year is an integer -- try Year([TrainingDate])= " & Year(Date())
Really hoping for guidance.
I'm calculating a running 3 month total in an Access query. two of the DSUM variables are strings and the third is a date that i derive from date parts.
The string variables I passing in are working fine, but I'm having a hard time getting the date range to work in the statement.
Here's what's working:
Running_TOT: DSum(" [SumOfCNTRCTL_ADJSTMT_AMT] ","tbl_processed_accts","[PYR]='" & [PYR] & "'and [PT_TYPE_CD]='" & [PT_TYPE_CD] & "'")
The following needs to be added in a third and clause:
DateSerial([DSCHRG_YR],[DSCHRG_MNTH],1)
BETWEEN
DateAdd("m",-2,DateSerial([DSCHRG_YR],[DSCHRG_MNTH],1))
AND
DateAdd("m",1,DateSerial([DSCHRG_YR],[DSCHRG_MNTH],1))-1
I think I've read every DSUM post on the internet looking for ideas. Eternal gratitude for anyone who can lend a hand getting the syntax right..
Thanks.
I would like to iterate over the rows in my Payment table. User chose for what month and year has wants to book and I want to check in the each raw if this house was booked for this year and month. I want to compare if
HouseID == chosenHouseID && BookingMonth == chosenBookingMonth &&
BookingYear==chosenBookingYear.
If this is true it should pop out message box with info that house was already booked for this month. Also if user chose more than one month i.e. numMonths would be 3, it should increment value of the month (which is a text) it should go to the next value (if there is no next value then it should do mod 12) and do the checking again. Maybe it will be necessary to switch data type of BookingMonth to numeric?
However I hope I was clear what I want to do. I have experience with Java, C, Python and Visual Basic, but I did not do much in Access so it is quite confusing. I could not find the any useful info how to perform this operation. Please advise me on my issue.
Thank you
Yes, you definitely should store the [BookingMonth] as numeric. Maintaining a "month" column as text will be a nuisance in the long run, since "August"<"January" and "12"<"2". You'd have to do at least a certain amount of juggling to convert the text values to numeric values, so make like easy for yourself and just maintain them as numeric. (Note that you can always format them as text if you want to use them in reports.)
As for your search requirements, if the user supplies a [chosenBookingYear], [chosenBookingMonth], and [numberOfMonthsToBook] then you can use the VBA DateAdd function to derive [endOfBookingYear] and [endOfBookingMonth] safely, accounting for "next year" values...
endOfBookingYear = Year(DateAdd("m", numberOfMonthsToBook - 1, DateSerial(chosenBookingYear, chosenBookingMonth, 1)))
...and...
endOfBookingMonth = Month(DateAdd("m", numberOfMonthsToBook - 1, DateSerial(chosenBookingYear, chosenBookingMonth, 1)))
Finally, to perform the lookup without looping through individual rows you can concatenate [BookingYear] and [BookingMonth] together to create something like "2013/05" using...
BookingYear & "/" & Format(BookingMonth, "00")
...so then you can create a SELECT query something like this:
SELECT * FROM Payment
WHERE HouseID = chosenHouseID AND
(
(BookingYear & "/" & Format(BookingMonth, "00"))
BETWEEN (chosenBookingYear & "/" & Format(chosenBookingMonth, "00"))
AND (endOfBookingYear & "/" & Format(endOfBookingMonth, "00"))
)
I want to obtain a running sum in query. I'm using the following running sum formula
RunningSum: CCur(Nz(DSum("[Debit]","[Debit]","[CustomerID] =" & [CustomerID] & " AND [vDate] < " & [vDate] & "")))
But it is not working. My purpose is to obtain sum of Debit for all smaller than the current date field, something like this,
http://i.stack.imgur.com/0qoO7.jpg
After going through different threads, I could not find any solution for my problem. I don't know that how I can get the sum of older debit amounts if there is duplicate date.
I think the easiest thing will be to just refer to running sums for all the controls you want to add. For example "31 to 60 Days" is text29 on your report. Create a hidden control called, say, R31to60 and set to Running sum over group, then in the footer, put a text box and set the control source to:
=[R31to60]
It will show the last value for the running sum, that is, the total.
In design view, the highlight shows the running sum control and total. The control can be shrunk down and hidden.
In report view you can see the "total" field shows the last value for running sum.