How do I write the Criteria in Query Design to equal last month's data? - ms-access

I've looked on a couple different forums and I'm unable to find what I need they all have it listed in SQL View, and that is NOT what I want. I would like to do this in the Query Design as it is much easier for me.
I simply would like to make a query to display certain parameters of the database I maintain. And those parameters would equal last month's data.
I previously was able to successfully make a query displaying all information for the past year but can't figure out how to display just this past months.
The past year Criteria:
>DateAdd("yyyy",-1,Date())
Since that worked I tried doing this but it would not work for me:
>DateAdd("mmmm",-1,Date())
I'm sure it's something simple that I'm just not seeing here. Any help or recommendations are welcome.
Referencing the link provided by Fionnuala I have come up with
>DateAdd("m',-30,Date())
The problem being is that it queries all results for the past 30 days from Today's date. I wish to display only data from October ! While this can be done easily. I don't wish to manually go in this query every month and change certain parameters. I would like it to be automatic so or next month December I click on the query and it displays all 30 days of November's data. And there is no manual process of going back in and changing any of the criteria.

In the Query Designer you can use a Criteria: like this
>=DateSerial(Year(Date()),Month(Date())-1,1) And <DateSerial(Year(Date()),Month(Date()),1)
The corresponding SQL statement is
SELECT Donations.*
FROM Donations
WHERE (((Donations.DonationDate)>=DateSerial(Year(Date()),Month(Date())-1,1)
And (Donations.DonationDate)<DateSerial(Year(Date()),Month(Date()),1)));
If it was run on November 18, 2014 then it would effectively be
SELECT Donations.*
FROM Donations
WHERE (((Donations.DonationDate)>=DateSerial(2014,10,1)
And (Donations.DonationDate)<DateSerial(2014,11,1)));
Notes:
This query should be sargable and take advantage of an index on the date field (if one exists).
In case anyone is concerned about "month wrap-around", the DateSerial() function takes care of that for us. If the query was run in January 2015 then the first WHERE condition would indeed be ... >=DateSerial(2015,0,1) but that's okay because it returns 2014-12-01.

Related

Report builder 3.0 Using Reportitems!TexboxXX.Value sometimes creates multiple boxes. Why?

I have 6 Datasets each one is the same query but has a different WHERE clause based on employee type. They generate 6 tables. At the top of the report there is a summary table which uses reportitems!textboxXX.value to grab the totals for 2 particular fields from all the tables. I'm also using a StartDate and EndDate parameter. Each reportitems! expression in the table comes from a different dataset in the report if that is relevant.
When I run the report using dates from yesterday back to the 9th of May I get the desired output.
But when I go to the 8th I get multiple rows all the same.
and as I go even further back I get even more rows of the same information. To my knowledge nothing interesting or different happened on the 8th of May and the tables further down the report look exactly the same. Any idea what might be causing this? The design view looks like this if that helps. There's no grouping or filters on the table.
Still not certain about the mechanics behind this but I found a 'solution' so I figured I'd post it. I just made a new dataset for my summary tables where the query was simply SELECT 1. Now I get a single row every time.

In SSRS How to limit start-end time within 7 days

I have a Report, I hope that if you choose to end time more than 7 days of the start time is prompt error.
Rather than give your users two date parameters have a single parameter to select the reporting period. You can use a SQL query to generate a list of weeks and then allow them to select which week they want to see data for. That way they can't ever select more than 7 days.
Otherwise you can short circuit the SQL by adding a DATEDIFF() between the two parameters. You could use an IF statement for this but you'll need to ensure that it returns the same columns and data types or I think SSRS will error out.
Otherwise just add the DATEDIFF() check in the WHERE clause so it will return no rows if the parameters are too far apart.
You'll also want to create a textbox on the report and have it conditionally visible if the parameters are too far apart. Something like big red text explaining to the user that they have selected a date range that is too large.
But, I think showing error messages should be avoided when you can just adjust the choices offered to the user so that they can't choose something that is invalid.

query to show Active employees for a given time frame

I am a little new to building this but have come a long way.
I have built a db using Access 2007. I have a table that shows the employees info:
Lname
Fname
Status
HireDate
TermDate
(Status: they are either inactive (potential Hires), Active or Terminated)
I can run a query that will show me all the employees by hire date or run one to show term dates.
We would like to have a query that will give us a count of how many drivers are still there within a given month.
Say Joe Smith was hired on 01/01/2008 and was terminated on 05/15/2011. If I ran a report in 2011 on May 31st how would I need to build the query to show this employee as being there in the month of May?
I have used >=Date() and others. I could use between #05/01/2011# and #06/01/2011# in the criteria under TermDate but if there is not a date there, nothing shows up. I have even dropped down a line and added "Null" and still nothing or I get all the employees that are still there and the ones that was terminated before the dates. I'm not sure what I am doing wrong.
I'm unsure about the logic for the filter criteria on this one. I think your goal is to identify all drivers who were on staff during any part of May 2011. My best guess is you need at least 2 conditions to identify them.
HireDate prior to June 1, 2011
TermDate either Null or >= May 1, 2011
If those conditions are sufficient, the SQL could be fairly easy.
SELECT e.Lname, e.Fname
FROM employees_info AS e
WHERE
e.HireDate < #2011-6-1#
AND
(
e.TermDate Is Null
OR
e.TermDate >= #2011-5-1#
);
It sounds like you're building the query in Design View ... which is a good and helpful feature. However, it's difficult to describe how to build that query in Design View. So I suggest you create a new query, switch to SQL View and paste in that SQL text. Replace employees_info with your actual table name, and fix any field names I misspelled.
If that query runs without error, you can flip back and forth between Design and SQL view, make a change in one, and examine how it is represented in the other view.
The SQL doesn't have to be formatted the way I wrote it. I chose that way in hopes it would make the WHERE logic clear. And if you make changes to the query from Design View, Access will reformat the SQL as it sees fit. However, the formatting change should not break the query.
I used yyyy-m-d format for the literal date values. That format avoids any possible confusion over which parts represent day and month, such as whether #05-01-2011# is intended to represent May 1st or Jan 5th. However, when you alter the query, Access may change them to mm-dd-yyyy format. (Sometimes its "helpful" impulses are annoying.)
I'm puzzled about one point. It seems you have one record per employee. If that is so, and an employee can leave for any reason and be re-hired later, it would be difficult to capture the different employment terms in a single record. If you're facing that situation, you may need to revise table designs.
If I misinterpreted your data, please show us a brief data sample, and the output you want from the query based on that sample. Good luck with this.

Access 2010 Query for current month

I have built an Access database to keep track of Quality Assurance Monitors for our team. Our team has several leads that oversee the reps on the team.
I want to build a query the will return all of the QA's for a specified lead for the current month. I have criteria for specifying a lead and have got that to work, but every time I try to set the criteria for the current month, it ends up returning no results.
Searching Google has repeatedly suggested using the Month(Now()), but that doesn't work either.
How can I write this query?
Answer inspired by #Justin-rentmeester but refined to always return the desired results:
Add the following to the WHERE clause of your query:
MyDateField BETWEEN
DateSerial(Year(Date()),Month(Date()),1)
AND
DateAdd("s", -1, DateAdd("m", 1, DateSerial(Year(Date()),Month(Date()),1))), DateSerial(Year(Date()),Month(Date()),1)
I reccommend that you use SQL view to add this criterium.
This approach also works with dates that include times, and with months with less than 31 days.
Put this code on criteria field- Between Date()-Day(Now()-1) And Date()
I edit my answer!
Assuming workdate is your date field.
A shorter where clause can be
where Year(workDate) = Year(Date()) and Month(workDate) = Month(Date())
This will filter all records for the current month.
If you want to further filter for records up to today add
and workdate <= Date()
This may not be necessary if you are not storing future dated records in your table.
Good luck.
To do this in the Design view in Access, select all of the columns you want in the report and on the date column you want to restrict to the current month, for the Criteria put: Between
DateSerial(Year(Date()),Month(Date()),1)
And
DateSerial(Year(Date()),Month(Date()),31)

I want to display clients based on a five day week on a form (All 5 days at once)

I've got a list of clients who have certain tasks done on a weekly basis. Currently we use an excel spreadsheet that keeps track of this but I am in the works of automating it. In the process of moving this into our MS Access system, I have created a form that does this for a single day and can display a report that outputs in the manner I want the form to look but I cannot get the form to look like that so the user can see all the days of the week for all the clients at once. I suppose I could do this in a subform per client but it seems a bit messy...
The spreadsheet we currently use has Column headers that state the Date and the rows are sort of grouped up by client that are for the number of times the given action occured, the timestamp of when it happened and other various data that happened on that day.
For the life of me I cannot think of a way to view all the records for that given week, grouped in detail per client all at once on the form. I can think of some ways to do this through VBA with recordsets but would like to know is there is a simpler way to do this that is easier to maintain.
Is there a way to do this with the use of a query and a few small scale tricks that don't involve storing recordsets? Keep in mind that this is for Access 2000, I only wish we would upgrade to '03.
Why not use five subforms? One for today, another for tomorrow, etc for
the next five business days. – Tony Toews Sep 12 at 20:57
That is the answer I've gone with and it works BEAUTIFULLY with the setup we are using. Thanks for the wonderful idea!