Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

SQL query use variable instead of hard coded dates?

Posted on 2013-06-25
9
Medium Priority
?
1,309 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
[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
  • 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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 

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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Microsoft Access is a place to store data within tables and represent this stored data using multiple database objects such as in form of macros, forms, reports, etc. After a MS Access database is created there is need to improve the performance and…
Lotus Notes has been used since a very long time as an e-mail client and is very popular because of it's unmatched security. In this article we are going to learn about  RRV Bucket corruption and understand various methods to Fix "RRV Bucket Corrupt…
This Micro Tutorial will demonstrate in Google Sheets how to use the HYPERLINK function to create live links inside your spreadsheet.
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…

618 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