Solved

Add date range to query

Posted on 2011-09-13
8
180 Views
Last Modified: 2013-11-05
One of the experts helped me with an SQL query a couple of weeks ago.  Here is the result which works fine...

SELECT a.reason1, Count(a.reason1) AS CountOfreason1
FROM (select reason1 from tblJobsToAnalyzeFromSQLview
union all
select reason2 from tblJobsToAnalyzeFromSQLview
union all
select reason3 from tblJobsToAnalyzeFromSQLview
union all
select reason4 from tblJobsToAnalyzeFromSQLview
)  AS a
GROUP BY a.reason1
HAVING (((Count(a.reason1))>0));

But now I need to add a date range criteria somehow and don't know how to code it.  The form I'm using has two date type text boxes... txtStart and txtEnd.  The table is tblJobsToAnalyzeFromSQLview and the field the date range criteria has to work against is "LastInvoice".

Thoughts?
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
  • 4
  • 2
  • 2
8 Comments
 
LVL 120

Expert Comment

by:Rey Obrero (Capricorn1)
ID: 36533080
test this, change "form1" with the actual name of your form


SELECT a.reason1, Count(a.reason1) AS CountOfreason1
FROM (select reason1 from tblJobsToAnalyzeFromSQLview where LastInvoice between Forms!Form1!txtStart and Forms!Form1!txtEnd
union all
select reason2 from tblJobsToAnalyzeFromSQLview where LastInvoice between Forms!Form1!txtStart and Forms!Form1!txtEnd
union all
select reason3 from tblJobsToAnalyzeFromSQLview where LastInvoice between Forms!Form1!txtStart and Forms!Form1!txtEnd
union all
select reason4 from tblJobsToAnalyzeFromSQLview where LastInvoice between Forms!Form1!txtStart and Forms!Form1!txtEnd
)  AS a
GROUP BY a.reason1
HAVING (((Count(a.reason1))>0));
0
 
LVL 120

Expert Comment

by:Rey Obrero (Capricorn1)
ID: 36533146
or this

SELECT a.reason1, Count(a.reason1) AS CountOfreason1
FROM (select reason1 from tblJobsToAnalyzeFromSQLview where LastInvoice between cvdate(Forms!Form1!txtStart) and cvdate(Forms!Form1!txtEnd)
union all
select reason2 from tblJobsToAnalyzeFromSQLview where LastInvoice between cvdate(Forms!Form1!txtStart) and cvdate(Forms!Form1!txtEnd)
union all
select reason3 from tblJobsToAnalyzeFromSQLview where LastInvoice between cvdate(Forms!Form1!txtStart) and cvdate(Forms!Form1!txtEnd)
union all
select reason4 from tblJobsToAnalyzeFromSQLview where LastInvoice between cvdate(Forms!Form1!txtStart) and cvdate(Forms!Form1!txtEnd)
)  AS a
GROUP BY a.reason1
HAVING (((Count(a.reason1))>0));
0
 
LVL 26

Expert Comment

by:Nick67
ID: 36533203
A short note on dates, which I seem to be mixed up with a lot lately!
Access ALWAYS stores a Date and Time value.
Format just messes with what you see, not with what is actually stored.
What is stored is actually a double precision (CDbl) value.
The decimal represents the hours portion (.25 is 6AM, .5 is 12PM, .75 is 6PM)

Why is this important?

If you have been storing Now(), and getting it displayed as dd-MMM-yy or any other date only format the time part is still there it's just not displayed.
Now, a BETWEEN operator may not give you the results you expect
If you tell it ...Where myDate between #1-Sep-11# and #13-Sep-11#, what you are actually telling it is ...Where myDate between #1-Sep-11 12:00:00 AM# and #13-Sep-11 12:00:00 AM#
If you have been storing times inadvertently, or even deliberately, the fact that you get only those values equal to 13-Sep-11 12:00:00 AM and not for the rest of September 13th is a bit disconcerting.  If you had no values with a midnight time, you'd get no Sep 13th values

In plain English, when we ask for everything between the 1st and 13th, we expect to get everything from the 13th, not just those from the stroke of midnight.
Maybe you haven't used Now() or any other things that may have included a time portion.  In that case, Access sees the time portion as 12:00:00 AM across the board and your BETWEEN operator works intuitively.

But it's worth mentioning
0
Simplifying Server Workload Migrations

This use case outlines the migration challenges that organizations face and how the Acronis AnyData Engine supports physical-to-physical (P2P), physical-to-virtual (P2V), virtual to physical (V2P), and cross-virtual (V2V) migration scenarios to address these challenges.

 
LVL 120

Expert Comment

by:Rey Obrero (Capricorn1)
ID: 36533214
well, just in case, that is no big deal for you can always use

 datevalue(dateField) to get rid of the time portion of the date field.
0
 
LVL 26

Expert Comment

by:Nick67
ID: 36533267
<grin>
There are many ways to avoid stepping on a landmine...if you know it's there.
The fun-with-dates landmine tends to be one you don't realize is there...til you've already stepped on it.
Which is why I posted!
0
 

Author Comment

by:SteveL13
ID: 36535898
I made a mistake in my original question.  LastInvoice is actually Last Invoice (with a space).  So what would the code be for that?

--Steve
0
 
LVL 120

Accepted Solution

by:
Rey Obrero (Capricorn1) earned 500 total points
ID: 36535973
change this

    LastInvoice

with

     [Last Invoice]
0
 

Author Closing Comment

by:SteveL13
ID: 36536057
Perfect!!!
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Did you know that more than 4 billion data records have been recorded as lost or stolen since 2013? It was a staggering number brought to our attention during last week’s ManageEngine webinar, where attendees received a comprehensive look at the ma…
This article describes a method of delivering Word templates for use in merging Access data to Word documents, that requires no computer knowledge on the part of the recipient -- the templates are saved in table fields, and are extracted and install…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
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.

687 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