Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

SQL query use variable instead of hard coded dates?

Posted on 2013-06-25
9
Medium Priority
?
1,348 Views
Last Modified: 2013-06-28
Hi,

I'm pulling data from an SQL database via MS access  2010. The SQL query works fine with the date and time hard coded. However, the user needs to set the start and end date and time manually.
For this I'm using a date picker and formating the date/time returned to yyyy-mm-dd 00:00:00 which is stored in a string variable (although I think the 102 in the convert statement means yy.mm.dd??)

How would I use this variable instead of the hard coded date? I'm really no good with SQL!

Here is the end part of the SQL query

BETWEEN CONVERT(DATETIME, '2013-06-01 00:00:00', 102) AND CONVERT(DATETIME, '2013-06-07 00:00:00', 102)) AND  (TMSACMON.LOGNO IN (35, 36, 37, 5)) ORDER BY TMSACMON.ACDATE"

The SQL is being executed in VBA like this..

Set objMyCmd.ActiveConnection = objMyConn
       objMyCmd.CommandText = "Blah Blah"
        objMyCmd.CommandType = adCmdText
0
Comment
Question by:wildarmsdave
  • 5
  • 4
9 Comments
 
LVL 49

Expert Comment

by:PortletPaul
ID: 39276797
can you post the vba that contains the full query please? this will help you get a complete answer.

>> I think the 102 in the convert statement means yy.mm.dd
102 gives: YYYY.MM.DD

for the format you are providing (which is a good one) this would suffice:
CONVERT(DATETIME, '2013-06-01 00:00:00')

however, please be aware that "between" probably does not do what you expect it to
let's say in your date picker you select the First of May and 31st of May
> with the expectation all data for May will be chosen by the query

IF, the actual data has "date with time" then "between" will NOT choose all of May
it would miss all events that occurred during May 31

The most reliable method for choosing date ranges is a combination of >= with <
but you also need to add one day to May 31
e.g.

where fieldname >= convert(datetime,'2013-05-01 00:00:00')
and fieldname < convert(datetime,'2013-06-01 00:00:00') -- one day more added

another difference is that "between" includes the second mentioned datetime
but if you use less than, then this is (correctly) not included

for more on this topic please see: "Beware of Between"
0
 

Author Comment

by:wildarmsdave
ID: 39278090
Hi PortletPaul,

Thanks for the reply. The full SQL query is listed below (it is pretty long!) I hope this helps in some way?


Open in new window


objMyCmd.CommandText = "SELECT TMSACMON.ACDATE, TMSACMON.LOGNO, TERMDEF.LOCATION, TMSACMON.EMPREF, TMSACMON.INITIALS, TMSACMON.SURNAME, TMSACMON.ACDIR AS DIRECTION, TMSACMON.REASON FROM TMSACMON INNER JOIN TERMDEF ON TMSACMON.LOGNO = TERMDEF.REF WHERE(TMSACMON.ACDATE BETWEEN CONVERT(DATETIME, '2013-06-01 00:00:00', 102) AND CONVERT(DATETIME, '2013-06-07 00:00:00', 102)) AND  (TMSACMON.LOGNO IN (35, 36, 37, 5)) ORDER BY TMSACMON.ACDATE"


Open in new window

0
 
LVL 49

Expert Comment

by:PortletPaul
ID: 39278154
that is not long :) but I'm a klutz with VBA (been about 127 years since I wrote any)

It will be "something" like this (I think)
Dim dteStart As Date
Dim dteEnd As Date
dteStart = #2013-06-01#
dteStart = #2013-06-07#

objMyCmd.CommandText = "SELECT TMSACMON.ACDATE, TMSACMON.LOGNO, TERMDEF.LOCATION, TMSACMON.EMPREF, TMSACMON.INITIALS, TMSACMON.SURNAME, TMSACMON.ACDIR AS DIRECTION, TMSACMON.REASON" _
& "FROM TMSACMON INNER JOIN TERMDEF ON TMSACMON.LOGNO = TERMDEF.REF WHERE (" _
& "TMSACMON.ACDATE BETWEEN #' & #" & dteStart & "#" _
& "AND #" & dteEnd & "# ) AND (TMSACMON.LOGNO IN (35, 36, 37, 5)) ORDER BY TMSACMON.ACDATE"

Open in new window

your job would be to get the datepicker information into those variables
also not you may need to add 1 day to the end date (as discussed earlier)

I cannot promise my poor vba works by copy/paste I'm afraid.
but if not hopefully others will assist
0
Get your Disaster Recovery as a Service basics

Disaster Recovery as a Service is one go-to solution that revolutionizes DR planning. Implementing DRaaS could be an efficient process, easily accessible to non-DR experts. Learn about monitoring, testing, executing failovers and failbacks to ensure a "healthy" DR environment.

 

Author Comment

by:wildarmsdave
ID: 39278947
Excel likes the Query (i.e it the text doesn't turn red) but on running it I get an error saying "Incorrect syntax near the keyword INNER"  ????
0
 
LVL 49

Expert Comment

by:PortletPaul
ID: 39280091
Dim dteStart As Date
Dim dteEnd As Date
dteStart = #2013-06-01#
dteStart = #2013-06-07#

objMyCmd.CommandText = "SELECT TMSACMON.ACDATE, TMSACMON.LOGNO, TERMDEF.LOCATION, TMSACMON.EMPREF, TMSACMON.INITIALS, TMSACMON.SURNAME, TMSACMON.ACDIR AS DIRECTION, TMSACMON.REASON" _
& "FROM TMSACMON INNER JOIN TERMDEF ON TMSACMON.LOGNO = TERMDEF.REF WHERE (" _
& "TMSACMON.ACDATE BETWEEN #"  & dteStart & "#" _
& "AND #" & dteEnd & "# ) AND (TMSACMON.LOGNO IN (35, 36, 37, 5)) ORDER BY TMSACMON.ACDATE"

Open in new window

made a small change on line 8 (there were too many # symbols)
but I don't know if this fixes that error
0
 

Author Comment

by:wildarmsdave
ID: 39282516
Hi PortletPaul,

Thanks for this but I'm still getting the same error message?

Ta,

Dave.
0
 
LVL 49

Expert Comment

by:PortletPaul
ID: 39283285
sorry "I'm a klutz with VBA", if this question doesn't attract someone with the knowledge needed soon maybe open a new question
0
 

Author Comment

by:wildarmsdave
ID: 39283975
Hi PortletPaul,

I've fixed this (revised query below). I think the # character is only used in MSACCESS VBA but I could be wrong. Thank you for trying, you stll get the points for your hard work!

Ta,

Dave.

objMyCmd.CommandText = "SELECT TMSACMON.ACDATE, TMSACMON.LOGNO, TERMDEF.LOCATION, TMSACMON.EMPREF, TMSACMON.INITIALS, TMSACMON.SURNAME, TMSACMON.ACDIR AS DIRECTION, TMSACMON.REASON " & "FROM TMSACMON INNER JOIN TERMDEF ON TMSACMON.LOGNO = TERMDEF.REF WHERE (" & "TMSACMON.ACDATE BETWEEN '" & dteStart & "'" & " AND '" & dteEnd & "') AND (TMSACMON.LOGNO IN (35, 36, 37, 5)) ORDER BY TMSACMON.ACDATE"
0
 
LVL 49

Accepted Solution

by:
PortletPaul earned 2000 total points
ID: 39284018
great work Dave, v.pleased you got there.

I wouldn't normally put # I have to say, it was "SQL database via MS access" that had me. I seem to recall the reverse being the case in another question, so: "I'm confused on this point" :)

Cheers, Paul
0

Featured Post

 [eBook] Windows Nano Server

Download this FREE eBook and learn all you need to get started with Windows Nano Server, including deployment options, remote management
and troubleshooting tips and tricks

Question has a verified solution.

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

In this article, I’ll look at how you can use a backup to start a secondary instance for MongoDB.
Windows Explorer lets you open cabinet (cab) files like any other folder. In VBA you can easily handle normal files and folders, but opening and indeed creating cabinet files takes a lot more - and that's you'll find here.
This Micro Tutorial will demonstrate in Microsoft Excel how to add style and sexy appeal to horizontal bar charts.
This Micro Tutorial will demonstrate the scrolling table in Microsoft Excel using the INDEX function.

810 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