Solved

Using an Access SQL query for two non-related tables based on a date range

Posted on 2014-09-17
4
355 Views
Last Modified: 2014-09-17
Good afternoon, I have an access database that has a query (this could also be a table) consisting of a date field (DueDate).  To keep it simple, the fields are Project (string), Leader (string), DueDate (short date), and Priority (string)

Here are the values that can be put into an Access database to generate a solution for the request I'm intending to make:  Project is considered the primary key and for simplicity it's the name of the project.

Table:  tblProject
Fields:  (in order)  Project, Leader, DueDate, Priority
Values: (in order below to Fields shown above)
HR Metrics, Ed, 9/3/2014, high
GMLOS, Tom, 9/16/2014, high
Monthly Bad Debt, Ed, 9/19/2014, medium
Kronos Time Detail, Jeff, 9/20/2014, medium
Monthly generics, Jeff, 9/17/2014, high
Quarterly denial, Ed, 9/30/2014, medium
ED by facility, Tom, 9/22/2014, critical
Accounting report, Tom 9/26/2014, high
Finance report, Tom 9/24/2014,critical
Weekly task report, Jeff, 9/23/2014, low

I have another table with only 1 record in it:
table: tblDateSelection
ID: 1
TodayDate: 9/17/2014   (primary key)
BeginDateRange:  -2
EndDateRange: 6

Here's what I'm wanting to do...

I want to create a query that takes the tblProject and filter where the DateDue is between #9/17/2014# (-2) AND 9/17/2014 (+6) given from the 1 record in the tblDateSelection table.  In other words Due date is between (-2 days of 9/17/2014) and (6 days added to 9/17/2014)   ----hence it would become----   WHERE DateDue is Between #9/15/2014# AND #9/23/2014#.  I realize that the tblProject and tblDateSelection have no common field; however, maybe there's a way to do this between the two tables that I don't know about.

I'm sure memory values held in VBA code would do something, but would like to see how it can be done directly from a SQL statement directly stored in the Access database (Queries) for other reasons.

I want to use the tblDateSelection because I can change this table values when necesssary.  So with the data above shown for tblProject, I want to pull those records between 9/15 and 9/23 (as indicated in the tblDateSelection (-2 from today - 9/17/2014) and (+6 from today - 9/17/2014)


Thank you,
0
Comment
Question by:HRISTeam
  • 2
  • 2
4 Comments
 
LVL 47

Accepted Solution

by:
Dale Fye (Access MVP) earned 500 total points
ID: 40328907
Try:

SELECT P.* FROM tblProject as P,  tblDateSelection as DS
WHERE P.DueDate >= DateAdd("d", DS.BeginDateRange, DS.TodayDate)
AND P.DueDate <= DateAdd("d", DS.EndDateRange, DS.TodayDate)
0
 
LVL 1

Author Closing Comment

by:HRISTeam
ID: 40328926
Thanks!  Works perfectly.  Wonderful!
0
 
LVL 47

Expert Comment

by:Dale Fye (Access MVP)
ID: 40328940
As long as the 2nd table only contains one record, that won't be a problem.  If you start adding records to the 2nd table, you will need to add a field from that table to the query set, something like:

SELECT DS.TodayDate as WeekOf, P.* FROM tblProject as P,  tblDateSelection as DS
WHERE P.DueDate >= DateAdd("d", DS.BeginDateRange, DS.TodayDate)
AND P.DueDate <= DateAdd("d", DS.EndDateRange, DS.TodayDate)
0
 
LVL 1

Author Comment

by:HRISTeam
ID: 40328967
Thanks, that makes sense if something ever occurs like that.  Simply put, it would be only one updateable record in that tblDateSelection table.  For anyone viewing this in the future --- this works great if you want to have a "master" one record value holder.  In my case, I would update the record daily when the database opens (update the field TodayDate with Now() date and use the -2 to 6 range again.  BTW, this -2 to 6 is not "set in stone" which means it can change and I would change it in that ONE record in the tblDateSelection table.
0

Featured Post

Backup Your Microsoft Windows Server®

Backup 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.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Error in query expression 3 37
MS Access XML Export Query Setup Multiple Tag Values 15 30
Running sum query 6 33
MS Access 03, TransferText, decimal places 8 47
In the article entitled Working with Objects – Part 1 (http://www.experts-exchange.com/Microsoft/Development/MS_Access/A_4942-Working-with-Objects-Part-1.html), you learned the basics of working with objects, properties, methods, and events. In Work…
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…
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…
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…

867 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

16 Experts available now in Live!

Get 1:1 Help Now