Solved

Query designer criteria using month number

Posted on 2015-01-27
8
211 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 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
Back Up Your Microsoft Windows Server®

Back up all your Microsoft Windows Server – on-premises, in remote locations, in private and hybrid clouds. Your entire Windows Server will be backed up in one easy step with patented, block-level disk imaging. We achieve RTOs (recovery time objectives) as low as 15 seconds.

 
LVL 50

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 50

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 50

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

[Webinar] Code, Load, and Grow

Managing multiple websites, servers, applications, and security on a daily basis? Join us for a webinar on May 25th to learn how to simplify administration and management of virtual hosts for IT admins, create a secure environment, and deploy code more effectively and frequently.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Omit After Update event 5 45
Format a text field as percent 12 56
ACESS 2010 Query Criteria 23 55
MS Access - Autofill field with item description 6 52
It’s been over a month into 2017, and there is already a sophisticated Gmail phishing email making it rounds. New techniques and tactics, have given hackers a way to authentically impersonate your contacts.How it Works The attack works by targeti…
This article describes two methods for creating a combo box that can be used to add new items to the row source -- one for simple lookup tables, and one for a more complex row source where the new item needs data for several fields.
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…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

734 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