SQL query use variable instead of hard coded dates?

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
wildarmsdaveAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

PortletPaulEE Topic AdvisorCommented:
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
wildarmsdaveAuthor Commented:
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
PortletPaulEE Topic AdvisorCommented:
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
Powerful Yet Easy-to-Use Network Monitoring

Identify excessive bandwidth utilization or unexpected application traffic with SolarWinds Bandwidth Analyzer Pack.

wildarmsdaveAuthor Commented:
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
PortletPaulEE Topic AdvisorCommented:
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
wildarmsdaveAuthor Commented:
Hi PortletPaul,

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

Ta,

Dave.
0
PortletPaulEE Topic AdvisorCommented:
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
wildarmsdaveAuthor Commented:
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
PortletPaulEE Topic AdvisorCommented:
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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Query Syntax

From novice to tech pro — start learning today.