Solved

Add date range to query

Posted on 2011-09-13
8
178 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
Complete VMware vSphere® ESX(i) & Hyper-V Backup

Capture your entire system, including the host, with patented disk imaging integrated with VMware VADP / Microsoft VSS and RCT. RTOs is as low as 15 seconds with Acronis Active Restore™. You can enjoy unlimited P2V/V2V migrations from any source (even from a different hypervisor)

 
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

Comprehensive Backup Solutions for Microsoft

Acronis protects the complete Microsoft technology stack: Windows Server, Windows PC, laptop and Surface data; Microsoft business applications; Microsoft Hyper-V; Azure VMs; Microsoft Windows Server 2016; Microsoft Exchange 2016 and SQL Server 2016.

Question has a verified solution.

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

Access custom database properties are useful for storing miscellaneous bits of information in a format that persists through database closing and reopening.  This article shows how to create and use them.
In Part II of this series, I will discuss how to identify all open instances of Excel and enumerate the workbooks, spreadsheets, and named ranges within each of those instances.
Familiarize people with the process of utilizing SQL Server stored procedures from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Micr…
With Microsoft Access, learn how to start a database in different ways and produce different start-up actions allowing you to use a single database to perform multiple tasks. Specify a start-up form through options: Specify an Autoexec macro: Us…

730 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