Results 1 to 4 of 4
  1. #1
    mahmud1180 is offline Novice
    Windows 7 64bit Access 2007
    Join Date
    Sep 2014
    Posts
    20

    query with a date parameter

    The following query works in Acess Vba.

    Query2 = "SELECT Futures.Date, Futures.PX_OPEN, Futures.PX_HIGH, Futures.PX_LOW, Futures.PX_SETTLE FROM Futures WHERE (((Futures.Date) Between #12/31/2013# And #2/28/2014#));"


    But If I get the value of date from excel it gives an error. I have tried putting it like that:

    Dim D1 As Date
    Dim D2 As Date



    D1 = y.Range("A1").Value
    D2 = y.Range("A2").Value

    Query2 = "SELECT Futures.Date, Futures.PX_OPEN, Futures.PX_HIGH, Futures.PX_LOW, Futures.PX_SETTLE FROM Futures WHERE (((Futures.Date) Between #D1# And #D2#));"

    This code gives me an error. I also tried using date number; still get the same error. Any suggestion?



    Thanks

  2. #2
    ranman256's Avatar
    ranman256 is offline VIP
    Windows Vista Access 2010 32bit
    Join Date
    Apr 2014
    Location
    Kentucky
    Posts
    9,527
    You have to separate the variable from the string:

    Query2 = "SELECT Futures.Date, Futures.PX_OPEN, Futures.PX_HIGH, Futures.PX_LOW, Futures.PX_SETTLE FROM Futures WHERE (((Futures.Date) Between #" & D1 & "# And #" & D2 & "#));"

  3. #3
    JoeM is offline VIP
    Windows XP Access 2007
    Join Date
    Jun 2012
    Posts
    3,904
    If you want to pass in the value of the variables, then cannot be placed inside the literal text, or else they are treated as literal text (literally "D1" and "D2").

    Try:
    Code:
    Query2 = "SELECT Futures.Date, Futures.PX_OPEN, Futures.PX_HIGH, Futures.PX_LOW, Futures.PX_SETTLE FROM Futures WHERE (((Futures.Date) Between #" & D1 & "# And #" & D2 & "#));"

  4. #4
    mahmud1180 is offline Novice
    Windows 7 64bit Access 2007
    Join Date
    Sep 2014
    Posts
    20
    Thank you guys. It worked

Please reply to this thread with any new information or opinions.

Similar Threads

  1. Replies: 2
    Last Post: 04-04-2013, 03:13 PM
  2. Replies: 6
    Last Post: 02-28-2013, 05:38 PM
  3. Parameter Query Date/Time
    By tcarp in forum Queries
    Replies: 1
    Last Post: 02-13-2013, 02:39 AM
  4. Date parameter
    By faraz in forum Access
    Replies: 1
    Last Post: 09-09-2011, 07:44 AM
  5. Parameter Date - Force a Monday Date
    By ker9 in forum Queries
    Replies: 3
    Last Post: 07-26-2011, 01:06 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Other Forums: Microsoft Office Forums