Results 1 to 6 of 6
  1. #1
    VariableZ's Avatar
    VariableZ is offline Partially Knowledgeable
    Windows Vista Access 2007
    Join Date
    May 2011
    Location
    Georgia, USA
    Posts
    40

    DCount Mixed With Format and Now Functions Causing Syntax Error

    I'm using MS Access 2007.


    I have fields in query "qryLotData" named "FormattedDate" (derived via CDate([datefield])) and "Dateweek" (derived from Format([FormattedDate],"ww")).
    I'm trying to use the DCount function in a form to count the number of entries week-to-date. My formula in the Control Source field is as follows:

    =DCount("[FormattedDate]","qryLotData","[DateWeek]=Format(Now(),"ww")")

    I keep getting the error message "The expression you entered contains invalid syntax. You may have entered an operand without an operator." I'm pretty sure that's not the case, and Microsoft's database tells me I should be able to do this, but I can't seem to figure out what's wrong. Any suggestions?

  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
    Either switch the quotes around ww to single quotes or:

    =DCount("[FormattedDate]","qryLotData","[DateWeek]=" & Format(Now(),"ww"))
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  3. #3
    VariableZ's Avatar
    VariableZ is offline Partially Knowledgeable
    Windows Vista Access 2007
    Join Date
    May 2011
    Location
    Georgia, USA
    Posts
    40
    Quote Originally Posted by pbaldy View Post
    ...switch the quotes around ww to single quotes...
    I changed the quotes to single quotes and it worked. Do you know why it's necessary sometimes to use single quotes instead of double quotes?

  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
    Sure; with this:

    "[DateWeek]=Format(Now(),"ww")"

    You started a string before DateWeek and intended to end it after the closing parentheses. You know by looking at it that the quotes around ww are just for the Format() function. Access isn't that smart. When you start a string with double quotes, it assumes the next set of double quotes ends that string. In this case, it was the quotes at the start of ww. That left everything after that more or less out in the middle of nowhere. It gets confused and throws an error. Using single quotes around the ww tells it that what follows is something different and not to close off the main string yet.

    Clear as mud now?
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  5. #5
    VariableZ's Avatar
    VariableZ is offline Partially Knowledgeable
    Windows Vista Access 2007
    Join Date
    May 2011
    Location
    Georgia, USA
    Posts
    40
    I suppose it does make sense. Thank you for the explanation. I'll be keeping that in mind from now on every time I make a formula.

  6. #6
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,521
    No problemo.
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

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

Similar Threads

  1. Query Criteria Causing Data Mismatch Error
    By jrubenol in forum Queries
    Replies: 2
    Last Post: 09-14-2011, 09:34 AM
  2. New tables created in a loop causing error
    By shubhamgandhi in forum Programming
    Replies: 2
    Last Post: 07-20-2011, 05:16 PM
  3. DCount Causing Form to Stall
    By Zerdan in forum Forms
    Replies: 2
    Last Post: 06-01-2011, 11:28 AM
  4. Trouble with DCount syntax.
    By jessica.ann.meade in forum Database Design
    Replies: 9
    Last Post: 02-18-2011, 10:09 AM
  5. dcount syntax correct?
    By lbgtp in forum Reports
    Replies: 2
    Last Post: 12-29-2010, 10:01 AM

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