Solved

Add date range to query

Posted on 2011-09-13
8
174 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
  • 4
  • 2
  • 2
8 Comments
 
LVL 119

Expert Comment

by:Rey Obrero
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 119

Expert Comment

by:Rey Obrero
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
 
LVL 119

Expert Comment

by:Rey Obrero
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
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
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 119

Accepted Solution

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

    LastInvoice

with

     [Last Invoice]
0
 

Author Closing Comment

by:SteveL13
ID: 36536057
Perfect!!!
0

Featured Post

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)

Join & Write a Comment

Regardless of which version on MS Access you are using, one of the harder data-entry forms to create is one where most data from previous entries needs to be appended to new records, especially when there are numerous fields and records involved.  W…
In a multiple monitor setup, if you don't want to use AutoCenter to position your popup forms, you have a problem: where will they appear?  Sometimes you may have an additional problem: where the devil did they go?  If you last had a popup form open…
Familiarize people with the process of utilizing SQL Server views 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 Microsoft Access…
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.

762 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

19 Experts available now in Live!

Get 1:1 Help Now