Results 1 to 6 of 6
  1. #1
    cbende2's Avatar
    cbende2 is offline Competent Performer
    Windows 7 32bit Access 2013
    Join Date
    Jun 2014
    Location
    Louisiana
    Posts
    370

    Question Error in code

    Hello all,

    I'm getting this error:

    The expression you entered as a query parameter produced this error: 'i'

    Can I not use my declared variable "i" in Dcount?



    Code:
    For i = 0 To 51
    
        O(1, i + 1) = DCount("Field1", "Table1", "DateValue([Field1]) Between DateSerial(Forms!Form3!txtYear-1,Forms!Form3!txtMonth,Forms!Form3!txtDay)+(i*7) And DateSerial(Forms!Form3!txtYear-1,Forms!Form3!txtMonth,Forms!Form3!txtDay)+6+(i*7)")
        Me.Controls("Text" & (i + 1)).Value = O(1, i + 1)
    
    
    Next i
    Thanks

  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,642
    Well, you can't use it outside VBA, and the DCount() is passing what you've given it to the database engine. You have to concatenate the values into the string, like:

    DCount("Field1", "Table1", "DateValue([Field1]) Between #" & DateSerial(Forms!Form3!txtYear-1,Forms!Form3!txtMonth,Forms!Form3!txtDay)+(i*7) & "# And #" & DateSerial(Forms!Form3!txtYear-1,Forms!Form3!txtMonth,Forms!Form3!txtDay)+6+(i*7) & "#")
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  3. #3
    cbende2's Avatar
    cbende2 is offline Competent Performer
    Windows 7 32bit Access 2013
    Join Date
    Jun 2014
    Location
    Louisiana
    Posts
    370
    Thanks Paul, that did the trick.

    While we're here I have another question.

    Field1 is a ShortText field hence the DateSerial(), and Some values in records are "N/A" and some are Null with no value. Is there anyway to skip these records when counting? I'm getting an error saying data mismatch.

  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,642
    If the field is text try using ' instead of # as the delimiter, but if I had to bet the error is coming from the DateValue() function. I personally would use the date/time data type. Not sure how to work around having text in there. I suppose one way would be to create a query that only returned date values and run your DCount() against that.
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  5. #5
    cbende2's Avatar
    cbende2 is offline Competent Performer
    Windows 7 32bit Access 2013
    Join Date
    Jun 2014
    Location
    Louisiana
    Posts
    370
    Ahhh great thinking! I'll try your second suggestion.

  6. #6
    June7's Avatar
    June7 is offline VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    53,641
    Otherwise use an IIf in the expression to handle the different possible values.

    DateValue(IIf(IsDate([Field1]), [Field1], #1/1/1900#))

    CDate() also works. I don't know if it has advantage/disadvantage over DateValue().
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

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

Similar Threads

  1. Error in VBA Code
    By guillermoftw in forum Access
    Replies: 3
    Last Post: 02-10-2015, 12:12 PM
  2. error with a code
    By ashraf_al_ani in forum Forms
    Replies: 3
    Last Post: 08-04-2014, 02:56 AM
  3. VBA Code Returning Error Run Time Error 3061
    By tgwacker in forum Access
    Replies: 2
    Last Post: 11-24-2013, 11:00 AM
  4. VBA code exiting no error.
    By Jmeyering in forum Programming
    Replies: 1
    Last Post: 11-12-2012, 04:29 PM
  5. Error in Code
    By Lockrin in forum Programming
    Replies: 3
    Last Post: 02-25-2010, 03:27 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