[Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Add date range to query

Posted on 2011-09-13
8
Medium Priority
?
184 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 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
NEW Veeam Agent for Microsoft Windows

Backup and recover physical and cloud-based servers and workstations, as well as endpoint devices that belong to remote users. Avoid downtime and data loss quickly and easily for Windows-based physical or public cloud-based workloads!

 
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 2000 total points
ID: 36535973
change this

    LastInvoice

with

     [Last Invoice]
0
 

Author Closing Comment

by:SteveL13
ID: 36536057
Perfect!!!
0

Featured Post

Free Backup Tool for VMware and Hyper-V

Restore full virtual machine or individual guest files from 19 common file systems directly from the backup file. Schedule VM backups with PowerShell scripts. Set desired time, lean back and let the script to notify you via email upon completion.  

Question has a verified solution.

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

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…
Code that checks the QuickBooks schema table for non-updateable fields and then disables those controls on a form so users don't try to update them.
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.
With Microsoft Access, learn how to specify relationships between tables and set various options on the relationship. Add the tables: Create the relationship: Decide if you’re going to set referential integrity: Decide if you want cascade upda…
Suggested Courses

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