Query designer criteria using month number

I have a query that has a field named "DueDate".  In this date field is for example 01/15/2015 or 03/25/2016 or 10/23/2017.

I also have a form where the user selects a month from a combobox.     So January is 1, February is 2, etc.

If the user selects, for example, March, then I want the query to return all records that have a DueDate of March (any day in March) OR BEFORE.

What would the query criteria be for the DueDate field?  The form they use to select the month is frmSelectMonth and the combobox is named cboSelectMonth.  Also when they select the month name from the combobox a text field named txtMonthN fills in with the month number if that is needed for this.

I hope I haven't confused everyone.
Who is Participating?

[Webinar] Streamline your web hosting managementRegister Today

GrahamMandenoConnect With a Mentor Commented:
When you say "any day in March OR BEFORE" do you want any day in January, February, or March of any year, or do want any day before the end of March for a particular year?

If it is the first option then your filter clause should be:
    WHERE Month([DueDate])<=Forms![YourForm]![YourCombo]

If it is the second option (which seems to make more sense) then how do you determine the particular year?  Is it this year, or last year, or does it depend on whether the given month has passed in the current year?

If, for example, you treat the month as the current or next occurrence of that month then you could use the following filter clause:
    WHERE [DueDate]<DateSerial(Year(Date()) + IIf(Forms![YourForm]![YourCombo]<Month(Date()), 1, 0), Forms![YourForm]![YourCombo]+1, 1)

If the current date is in June 2015, entering 8 will give you all dates before 1 September 2015, while entering 4 will  give you all dates before 1 May 2016.

If your requirement is not one of the above, then please post back with more info.

Graham Mandeno [Access MVP 1996-2015]
In the query, include a column as follows


Criteria: <=Forms![frmSelectMonth]![txtMonthN]
Dale FyeCommented:
Well, simply using the month would give you March 2015, March 2016, March 2017.  So, I generally create my combo boxes to display month and year.  I use a table (tbl_Numbers) which has a single field (lng_Number) and 100 records (the values 0 - 99).  Then I create a query for my combo that looks something like (this particular query looks back 6 months and forward 12 months):

SELECT DateSerial(Year(Date()), Month(Date()) - 6 + lng_Number, 1) as FirstDayOfMonth
, Format(DateSerial(Year(Date()), Month(Date()) - 6 + lng_Number, 1), "mmm yy") as YrMon
FROM tbl_Numbers
ORDER BY DateSerial(Year(Date()), Month(Date()) - 6 + lng_Number, 1)

Then I set the default value of that combo to the current month
Default: DateSerial(Year(Date()), Month(Date()), 1)

BoundColumn: 2
Column Widths: 0;.75

Then, the criteria in your query
WHERE Format([DueDate], "mmm yy") = [Forms]![yourForm]![cboYrMon]
The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

Gustav BrockCIOCommented:
That would be:

Where DueDate < DateSerial(Year(Date()), Val([Forms]![frmSelectMonth]![txtMonthN]) + 1, 1)

Hi Gustav

No - I was adding one to the current year number if the given month is less that the current month - i.e. the next instance of the given month.

Gustav BrockCIOCommented:
Sorry, it was not a comment to your code but the answer to the original question.

SteveL13Author Commented:
Adding this to the query did exactly what I needed it to do:

WHERE Month([DueDate])<=Forms![YourForm]![YourCombo]
Gustav BrockCIOCommented:
If you have data for several years, it will probably not be exactly that.

All Courses

From novice to tech pro — start learning today.