Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Query designer criteria using month number

Posted on 2015-01-27
8
Medium Priority
?
216 Views
Last Modified: 2015-01-30
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.
0
Comment
Question by:SteveL13
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
8 Comments
 
LVL 18

Expert Comment

by:Simon
ID: 40574050
In the query, include a column as follows

Expr1:month(DueDate)

Criteria: <=Forms![frmSelectMonth]![txtMonthN]
0
 
LVL 48

Expert Comment

by:Dale Fye
ID: 40574190
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]
0
 
LVL 20

Accepted Solution

by:
GrahamMandeno earned 2000 total points
ID: 40574432
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]
0
Nothing ever in the clear!

This technical paper will help you implement VMware’s VM encryption as well as implement Veeam encryption which together will achieve the nothing ever in the clear goal. If a bad guy steals VMs, backups or traffic they get nothing.

 
LVL 51

Expert Comment

by:Gustav Brock
ID: 40574783
That would be:

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

/gustav
0
 
LVL 20

Expert Comment

by:GrahamMandeno
ID: 40574926
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.

Cheers,
Graham
0
 
LVL 51

Expert Comment

by:Gustav Brock
ID: 40574930
Sorry, it was not a comment to your code but the answer to the original question.

/gustav
0
 

Author Closing Comment

by:SteveL13
ID: 40579843
Adding this to the query did exactly what I needed it to do:

WHERE Month([DueDate])<=Forms![YourForm]![YourCombo]
0
 
LVL 51

Expert Comment

by:Gustav Brock
ID: 40579849
If you have data for several years, it will probably not be exactly that.

/gustav
0

Featured Post

NEW Veeam Agent for Microsoft Windows

Backup and recover physical and cloud-based servers and workstations, as well as endpoint devices that belong to remote users. Avoid downtime and data loss quickly and easily for Windows-based physical or public cloud-based workloads!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

As tax season makes its return, so does the increase in cyber crime and tax refund phishing that comes with it
AutoNumbers should increment automatically, without duplicates.  But sometimes something goes wrong, and the next AutoNumber value is a duplicate.  This article shows how to recover from this problem.
Learn how to number pages in an Access report over each group. Activate two pass printing by referencing the pages property: Add code to the Page Footers OnFormat event to capture the pages as there occur for each group. Use the pages property to …
In Microsoft Access, learn different ways of passing a string value within a string argument. Also learn what a “Type Mis-match” error is about.

705 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question