Results 1 to 8 of 8
  1. #1
    craig1988 is offline Advanced Beginner
    Windows XP Access 2007
    Join Date
    Aug 2014
    Posts
    82

    Syntax error with where criteria

    Count = DCount("Reference", "tblLog", "[DateLogged] Between """ & strDate & "'" AND "'" & endDate")



    I am getting a syntax error when this line of code tries to run. Can anyone point out where the error is? The variables strDate & endDate are declared and set to txt box values on a form.

  2. #2
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,521
    Try

    Count = DCount("Reference", "tblLog", "[DateLogged] Between #" & strDate & "# AND #" & endDate & "#")
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  3. #3
    cyanidem's Avatar
    cyanidem is offline Competent Performer
    Windows 10 Access 2010 64bit
    Join Date
    Nov 2015
    Location
    Consett, UK
    Posts
    270
    AFAIK you can't use between for dates. Try instead:
    Count = DCount("Reference", "tblLog",
    "[DateLogged] >=#" & strdate & "# And [DateLogged] <=#" & enddate & "#")

  4. #4
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,521
    Quote Originally Posted by cyanidem View Post
    AFAIK you can't use between for dates.
    ?DCount("*","Manual", "RevisionDate Between #1/1/2015# And #12/31/2015#")
    404
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  5. #5
    craig1988 is offline Advanced Beginner
    Windows XP Access 2007
    Join Date
    Aug 2014
    Posts
    82
    No issues with the between!!

    The issue was with the placing of the #'s and the "'s

    Works great now. Thanks

  6. #6
    cyanidem's Avatar
    cyanidem is offline Competent Performer
    Windows 10 Access 2010 64bit
    Join Date
    Nov 2015
    Location
    Consett, UK
    Posts
    270
    Yeah, I was wrong, but I swear I read somewhere that "between" works only for numeric values, not dates. Maybe it was about SQL...?
    I'll see myself out

  7. #7
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,521
    Happy to help!

    cyanidem, you might be thinking of the fact that Between doesn't work in VBA code directly, like:

    If Blah Between 1 And 5 Then

    It works in this situation because the DCount() is passed to the database engine to process. That's in layman's terms, because I don't know the actual technical terms.
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  8. #8
    cyanidem's Avatar
    cyanidem is offline Competent Performer
    Windows 10 Access 2010 64bit
    Join Date
    Nov 2015
    Location
    Consett, UK
    Posts
    270
    Yeah, could be that, or something completely different Nevertheless I should get my facts straight before advising others.
    But, on the plus side, my solution would work too

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

Similar Threads

  1. Replies: 2
    Last Post: 01-04-2016, 09:40 AM
  2. Replies: 3
    Last Post: 01-23-2014, 07:49 AM
  3. Replies: 6
    Last Post: 05-30-2012, 12:32 PM
  4. Incomplete Syntax Clause (syntax error)
    By ajetrumpet in forum Programming
    Replies: 4
    Last Post: 09-11-2010, 10:47 AM
  5. using FindFirst with a Date Criteria (syntax error)
    By ajetrumpet in forum Programming
    Replies: 2
    Last Post: 09-10-2010, 11:01 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