Solved

Query designer criteria using month number

Posted on 2015-01-27
8
199 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:SimonAdept
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
 
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
Complete VMware vSphere® ESX(i) & Hyper-V Backup

Capture your entire system, including the host, with patented disk imaging integrated with VMware VADP / Microsoft VSS and RCT. RTOs is as low as 15 seconds with Acronis Active Restore™. You can enjoy unlimited P2V/V2V migrations from any source (even from a different hypervisor)

 
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

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

This article is a continuation or rather an extension from Cascading Combos (http://www.experts-exchange.com/A_5949.html) and builds on examples developed in detail there. It should be understandable alone, but I recommend reading the previous artic…
In a multiple monitor setup, if you don't want to use AutoCenter to position your popup forms, you have a problem: where will they appear?  Sometimes you may have an additional problem: where the devil did they go?  If you last had a popup form open…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
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.

762 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now