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)
Related
Using Access 2007
This is my first question and i apologize in advance if anything is asked "the wrong way".
I have a huge database regarding invoices at work.
We want to have a counter showing "How many invoices did we recieve yesterday, if it's monday, how many did we recieve on friday, saturday and sunday?"
The counter itself is no issue, but the query criteria to make it get multiple days is really annoying me (lack of knowledge)
It seems to be subtracting 3 days regardless of what day it is.
Why is the following not working in query criteria? (Not returning friday, saturday and sunday values when monday and yesterdays results if NOT monday?)
IIf(Weekday(Date()=2);<=Date()-3;Date()-1)
Edit: I'm using a danish version of Access 2007, which is why there's ; instead of ,
You can't use the result of IIf() as full criteria expression, but you can use it as criteria parameter. Use a WHERE clause like this:
WHERE (DateDiff("d",[InvoiceDate],Date()) <= IIf(Weekday(Date())=2,3,1))
AND (DateDiff("d",[InvoiceDate],Date()) >= 1)
In the query design editor both criteria will be combined into one column (German Access here).
Omit AND (DateDiff("d",[InvoiceDate],Date()) >= 1 if you want to include invoices from today.
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.
I am trying to create a query on a table that filters out dates. I need to display all records previous to a specific date, but need this to be dynamically updated annually.
Specifically, I need to find dates prior to October 1 of the previous year. (i.e. anything prior to Oct 1, 2012.)
I know I can use '<#10/1/2012#' but would like to make this dynamic to update every year, as this same function will be utilized for multiple queries.
I have tried several iterations of the Date() function to get this to work, to no avail.
Things that don't work are...
Year([TrainingDate])
[TrainingDate] < DateSerial(Year(Date())-1,10,1)
Date() is the current date, so Year(Date()) is the current year.
In MS Access I am trying to filter to show only records from previous week.
I have colums showing weeknumber based by record date.
Using DatePart("ww",Date(),2,2)-1 in criteria field works well if current week is not 1. It does not work to previous year.
How I can show only records from previous week or 2 weeks, even if it goes to previous year?
For the most recent week of data, start with something like this ...
YourDateField Between (Date() -7) AND Date()
That approach will not break when your target YourDateField range include dates from 2 years. Another advantage of that approach is the query can make use of an index on YourDateField, if you have one. In that case the db engine can examine the index to find which rows match your criterion and read only the matching rows instead of all rows in the record source. (If you don't have an index on YourDateField, add one to see if it speeds up the query significantly. You can drop the index if it's ineffective.)
Conversely using a WHERE condition based on the DatePart() function will force the db engine to examine each and every row in the query's record source. That can be a performance bottleneck which can be significant with a huge table, especially if it is a linked table which requires data be read across a network connection.
Edit: I misunderstood what you wanted. For the the week which started on the previous Monday, I used this condition:
Between Date() - ( 6 + Weekday(Date(), 2))
AND Date() - Weekday(Date(), 2)
The db engine will evaluate those expressions one time only when running the query, so while this approach is more complicated than what I first suggested, it can still offer the potential performance improvements over using DatePart() for every row in the data source.
HansUp gave me an idea to solve my problem.
Using Between (Date()-7-Weekday(Date(),2)+1) And (Date()-Weekday(Date(),2)+1), should give me previous calendar week. Weekday() returns daynumber of the week. Substracting weekday number (+1) from date should return Monday of the week.
I am using an MS Access db to track some tasks during the year. Each task has a due Month. I do not want to use exact dates as the convention in my team is to refer to the month. I don't want to store the dates in a date format as team members will be entering the due month by hand.
Is it possible to sort my fields in date order if the date is stored as a text string month? (eg. January, February rather than 31/01/2009, 28/02/2009).
If so, what would such a query look like?
Thanks in advance.
If you are storing only the month name, your will first need to convert to a date to get a month number, use a lookup table (MonthNo, MonthName) or use the Switch function. Here is an example of converting to a date:
SELECT Month(CDate(Year(Date()) & "/" & [MonthNameField] & "/1")) AS MonthNo
FROM Table
However, there is probably a good argument for storing a date based on the month name entered, this would prevent any confusion about years.
This should work
SELECT *
FROM TableName
OrderBy Month(date_field)
I would store the month as an integer 1-12 then you can easily sort them.
I would make a date field.
I would store 1/1/2009 for January 2009, 2/1/2009 for February 2009, and so forth. For display purposes, I'd format it so that it displayed only the month (or Month + Year -- can't imagine how you wouldn't want the year).
This makes it possible to take advantage of date operations on the field without messy conversions of text to date formats.
Thank you all for your responses. Sorry for the delay in responding - I'm working on this issue again now.
For clarity, the DB is to be used to track a schedule of events within a 12 month period. The year does not need to be stored as everything in the DB is referring to the same year. A new copy of the DB will be made at the beginning of 2010.
I'm really keen to actually store the month as a word rather than any kind of value or date field as when bulk adding tasks I will likely edit the table directly rather than use a form.
I realise this is dead but google brought me here while i was looking so thought I would add to it:
I had this problem myself (Access 2010) and found a decent answer here: http://www.vbforums.com/showthread.php?503841-How-to-Convert-MonthName-to-Value(Microsoft-access-2003)
So what I did was have a Query which pulled out the DISTINCT months from my table. Then in the design view i added another column with MonthNo: Month(CDate("1 " & [Month]))and sorted the query on this column
hope this helps someone if not the OP.