Solved

Selecting records 11 months prior to StartDate

Posted on 2014-02-11
11
291 Views
Last Modified: 2014-02-11
Hi Experts

I have created a query with the following criteria and based on a table with the fields [StartDate] and [EndDate]…

Between DateAdd("m",-11,[StartDate]) And DateAdd("m",-1,[EndDate])

If I set the [StartDate] on 01/01/2014 and the [EndDate] on 31/01/2014, I get the correct records returned starting 01/02/2013 to the 31/12/2013 (11 months). However, if I set the [StartDate] to 01/02/2014 and [EndDate] to 28/02/2014 my query will only return those records from the 01/03/2014 to the 28/01/2014.

I would really appreciate some help on how to change the criteria to include records to the last day of the month for the [EndDate] (31/01/2014)? I've searched extensively and can't find anything that is helping.

Thanks
darls15
0
Comment
Question by:darls15
  • 4
  • 4
  • 3
11 Comments
 
LVL 120

Expert Comment

by:Rey Obrero (Capricorn1)
ID: 39852229
try this criteria


Between dateserial(Year([StartDate]), Month([StartDate])-11,Day([StartDate]) And dateserial(Year([EndDate]),Month([EndDate])-1,Day([EndDate]))
0
 
LVL 10

Accepted Solution

by:
Gozreh earned 250 total points
ID: 39852249
is your EndDate always the last day of the month ?  
if yes you can use "Between DateSerial(Year([StartDate]), Month([StartDate])-11,Day([StartDate]) And DateSerial(Year([EndDate]),Month([EndDate]),1)-1"
0
 

Author Comment

by:darls15
ID: 39852276
@Rey, thank you, however your query returned the only the records to the 24/01/2014. also, i placed a closing bracket before the And statement to get it to work, hope this was correct.
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.

 

Author Comment

by:darls15
ID: 39852281
hi gozreh, i've tested your solution and it works as i need it to. i also needed to put a closing bracket before the And statement. thank you.
0
 
LVL 120

Expert Comment

by:Rey Obrero (Capricorn1)
ID: 39852282
sorry, typo missing ")"

Between dateserial(Year([StartDate]), Month([StartDate])-11,Day([StartDate])) And dateserial(Year([EndDate]),Month([EndDate])-1,Day([EndDate]))


what are the startDate and EndDate that you selected?
0
 
LVL 10

Expert Comment

by:Gozreh
ID: 39852288
DateSerial(Year([EndDate]),Month([EndDate]),1)-1  this will always give the last date of the previous month.
0
 
LVL 120

Expert Comment

by:Rey Obrero (Capricorn1)
ID: 39852293
to get the last date of the Previous month, the syntax is

DateSerial(Year([EndDate]),Month([EndDate]),0)
0
 

Author Comment

by:darls15
ID: 39852295
hi rey, i set the [StartDate] to 01/02/2014 and [EndDate] to 28/02/2014. i tried this again and it is still only returning records from 01/03/2013 to 28/01/2014.
0
 
LVL 120

Assisted Solution

by:Rey Obrero (Capricorn1)
Rey Obrero (Capricorn1) earned 250 total points
ID: 39852307
try this


Between dateserial(Year([StartDate]), Month([StartDate])-11,Day([StartDate])) And DateSerial(Year([EndDate]),Month([EndDate]),0)
0
 

Author Closing Comment

by:darls15
ID: 39852335
thank you both. i have given equal points to both your solutions as both will be helpful to me. your help is appreciated.
0
 
LVL 10

Expert Comment

by:Gozreh
ID: 39852353
For your example if you need 11 months prior to date, you don't need a start date and end date, you can use = Between DateSerial(Year(Date()),Month(Date())-11,1) And DateSerial(Year(Date()),Month(Date()),0)
0

Featured Post

Ransomware-A Revenue Bonanza for Service Providers

Ransomware – malware that gets on your customers’ computers, encrypts their data, and extorts a hefty ransom for the decryption keys – is a surging new threat.  The purpose of this eBook is to educate the reader about ransomware attacks.

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
Preparing an email is something we should all take special care with – especially when the email is for somebody you may not know very well. The pressures of everyday working life stacked with a hectic office environment can make this a real challen…
Learn how to create and modify your own paragraph styles in Microsoft Word. This can be helpful when wanting to make consistently referenced styles throughout a document or template.
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…

839 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