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.
Related
My query name:
Total Daily Orders Summary
My Input [2 columns]:
business_date,# Daily Orders
I need these 3 columns as calculated output:
Orders# (7 days) Average, Orders# (30 Days) Average ,Orders# (Acuum Average) [i.e.from day one to date]
I have tried my best to find clear answers for how to do moving/rolling average in Microsoft Access query but unfortunately, I couldn't make it work for me. Therefore, I have decided to put my request here to see if someone will put me in the right direction to start working on my files and tasks.
I have trouble with correlated subqueries myself. here is a VBA/Access solution that I think is easier.
Start by adding a code module to your Database by clicking create on the menu and selecting module -which is all the way to the right.
Then create a public function like:
Public Function AverageOrders(CurrentDate As Date, NumberofPriorDays As Integer) As Integer
AverageOrders = DSum("DailyOrders", "MyTable", "business_date BETWEEN #" & CurrentDate & "# AND #" & DateAdd("d", -NumberofPriorDays, CurrentDate) & "#")
End Function
we create the function this way so access intellisense works. This is a powerful technique which I also use to wrap any calls to access form parameters. To find the function in a query right click on the top of a query column and choose build. In the build wizard under expression elements select functions then your database. Average Orders and any other public functions you make will appear. Or just type it.
then you get:
February has 28 days. We don't use # for DateOrders because In strings # denotes a date. To get the accumulated average just pick an absurdly early date or make another function without [NumberofPriorDays].
I am creating a Report in MS Access 2016. I want textboxes showing a summary of data from other tables - without actually showing rows of those tables (no subreports if possible). For example, I have tried to create a textbox with formula
=Format(Avg([WeekData].[DIFOT]),"##0.0%") & " DIFOT This Week"
which should return something like
100% DIFOT This Week
(NB Weekdata is a query and DIFOT is a field in that query, all in the same database as this report.)
However instead it just gives me #Error.
Please can you tell me the most efficient way to pull together summary figures such as these without creating any more queries and/or subreports than absolutely necessary? I'm quite new to SQL and Access on the whole.
Many thanks in advance.
Use DAvg() domain aggregate function. Also, the ## characters in Format() pattern serve no purpose.
=Format(DAvg("DIFOT", "WeekData"), "0.0%") & " DIFOT This Week"
or
=Format(DAvg("DIFOT", "WeekData"), "Percent") & " DIFOT This Week"
I am having an issue getting criteria to work. There is a related question on StackO here, but I have tried what's in it and it's still not working, so I'm asking my specific question. Apologies if that's not technically kosher at StackO.
Can't filter MS access datetime field using short date
I have a query that I need to have WHERE criteria based on an associate ID and the current day’s date. But the field that has the current day’s date needs to be a long date and time field, so criteria for short date doesn’t work. I have made this query in query design mode and it does exactly what I want:
SELECT tbl_Data.[#], AssocID, tsUpdated FROM tbl_Data WHERE AssocID = 4441 AND DateValue([tsUpdated])=Date()));
But when I do the equivalent in VBA, which I need to have this happen in, it does not work. Please note, I have used Date in this vba version because according to this website, Date will return current date:
Set FinishReport = CurrentDb.CreateQueryDef("qry_SessionReport", "SELECT tbl_Data.[#], AssocID, tsUpdated FROM tbl_Data WHERE AssocID = 4441 AND DateValue([tsUpdated])=Date”)
I have tried all kinds of syntax, and I have tried the answers from that StackO URL above, but nothing has worked. Any thoughts?
Thanks!
For the querydef, I noticed you don't have the closing parenthesis for the Date() function in the SQL text. In the VBA editor, the function is listed as Date with no parenthesis but if you tried to use the Date() function in a query without the parenthesis Access would throw an error. Hope that helps!
I'm really new to MS Access and got stuck a little problem which looks like i can't solve it.
Following I have a little example which makes it easier to explain.
As you can see I have a little calendar. Very easy and simple. I do a lookup on my tblCalendarMonths and on my tblCalendarYears. The field calDisplay is a calculated field an should put the Month and the Year together. In 'Picture 2' you can see how I tried it. In 'Picture 1' you can see what happens.
All I get is are the IDs of the selected Month and Year. What do I need to do,
to see under Display: "June 2014"
Thanks in advance!
Is there a reason you want to have the date separated like that in your table? If not you can use the DatePart Function to return the year and month as you desire.
DatePart("yyyy", [CalDate]) & " " & MonthName(DatePart("m", [CalDate]))
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] &"'")