Solved

Query designer criteria using month number

Posted on 2015-01-27
8
207 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
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 47

Expert Comment

by:Dale Fye (Access MVP)
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 500 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
The Eight Noble Truths of Backup and Recovery

How can IT departments tackle the challenges of a Big Data world? This white paper provides a roadmap to success and helps companies ensure that all their data is safe and secure, no matter if it resides on-premise with physical or virtual machines or in the cloud.

 
LVL 49

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 49

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 49

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

Enterprise Mobility and BYOD For Dummies

Like “For Dummies” books, you can read this in whatever order you choose and learn about mobility and BYOD; and how to put a competitive mobile infrastructure in place. Developed for SMBs and large enterprises alike, you will find helpful use cases, planning, and implementation.

Question has a verified solution.

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

Introduction When developing Access applications, often we need to know whether an object exists.  This article presents a quick and reliable routine to determine if an object exists without that object being opened. If you wanted to inspect/ite…
Introduction The Visual Basic for Applications (VBA) language is at the heart of every application that you write. It is your key to taking Access beyond the world of wizards into a world where anything is possible. This article introduces you to…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
Basics of query design. Shows you how to construct a simple query by adding tables, perform joins, defining output columns, perform sorting, and apply criteria.

861 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