473,406 Members | 2,713 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

Access SQL spanning years

Hi all,

How can I span two years using SQL?

I'm using Access 2007 and writing the queries directly in the SQL editor.

Here's an example of what I'm trying to accomplish:

I have a table, 'attendance' that tracks class attendance
Expand|Select|Wrap|Line Numbers
  1. CREATE TABLE attendance
  2. attendanceid INT AUTOINCREMENT,
  3. last_name TEXT,
  4. first_name TEXT,
  5. classmonth INT,
  6. classyear INT
  7. class_date DATE
I want to be able to write a query that asks how many times John Doe attended class in the last 5 months.

I can design the query specifying the user-defined parameter [Month]:
Expand|Select|Wrap|Line Numbers
  1. ...
  2. classyear = Year(date())
  3. AND classmonth BETWEEN ([Month] - 5) AND [Month]
There's no problem if I execute the query in September. But, if I execute the query in February there's a problem.

So... how can I write the query so that it automatically spans two years if needed?

I'd like to avoid solutions requiring custom VBA, if possible so that I only have to go to one place to edit the query. I don't mind using Access' functions (like Year(date()).

Thanks!
Mar 16 '12 #1
3 1563
NeoPa
32,556 Expert Mod 16PB
Use a Date field (You don't tell us, but I assume [Class_Date] contains the relevant info) :
Expand|Select|Wrap|Line Numbers
  1. strSQL = "... " & _
  2.          "WHERE ([Class_Date] >= #" & Format(DateAdd("m", -5, Date()),"m\/1\/yyyy") & "#)"
Mar 16 '12 #2
Thanks NeoPa!

I've been trying out your suggestion and it's working great!
Mar 18 '12 #3
NeoPa
32,556 Expert Mod 16PB
Very pleased to hear it :-)
Mar 18 '12 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

7
by: Billy Jacobs | last post by:
I am using a datagrid to display some data. I need to create 2 header rows for this grid with columns of varying spans. In html it would be the following. <Table> <tr> <td colspan=8>Official...
2
by: _mario.lat | last post by:
kruskal:minimum spanning tree. how to do? I'd like to find the minimum spanning tree with kruskal algorithm. There is a code (in C++) written? which contenitor do you suggest (Vector, set, ...)?...
6
by: Michael Bulatovich | last post by:
I have a very simple db I use for keeping track of hours, tasks, projects, clients etc. It has a form that I use to enter data. Currently the form has a textbox for a field called "start time",...
1
by: edo | last post by:
Following is a summary of articles spanning a 7 day period, beginning at 12 Dec 2003 05:22:35 GMT and ending at 19 Dec 2003 04:09:06 GMT. Notes ===== - A line in the body of a post is...
2
by: edo | last post by:
100 Day Analysis Following is a summary of articles spanning a 100 day period, beginning at 10 Sep 2003 06:41:30 GMT and ending at 19 Dec 2003 04:09:06 GMT. Notes ===== - A line in the...
4
by: Peter | last post by:
I have an application written in Access 2000 that I distribute to a number of organisations who use it on a variety of platforms, ranging from a P75 running Windows 95 to modern XP machines. It...
3
by: Coffmans | last post by:
Hello, I am new to this community and I apologize if I am posting this message under the wrong group. I am interested in writing a C# application that can be used to detect unauthorized wireless...
1
by: jenny22 | last post by:
i have a problem i am very new to c++ and want to construct a minimum spanning tree for 8 stocks i have calculated in excel the relevant formulas and know the weights of each of gthe vertices but...
0
by: olympics | last post by:
download britney spears videos http://gpirate.com/search?hl=en&q=britney+spears
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.