Results 1 to 7 of 7
  1. #1
    sdel_nevo is offline Competent Performer
    Windows 7 32bit Access 2010 32bit
    Join Date
    Apr 2013
    Location
    Gloucester, UK
    Posts
    402

    Dcount with "And" and "Or"

    hi guys

    I am having a hard time getting my head around this but basically I am trying to do a dcount on multiple criteria

    this is working as I would like

    Nz(DCount("ItemNumber", "UNEXData", "ItemNumber = forms.UNEXdetails.ITEMNUMBER And Planned = true And Running = False And Completed = false"), 0)



    this produces the correct number "1" which is correct

    what I can't seem to get my head around is using an or statement for example

    Nz(DCount("ItemNumber", "UNEXData", "ItemNumber = forms.UNEXdetails.ITEMNUMBER And Planned = true Or Running = true Or Completed = true"), 0)

    this produces the incorrect number "67" which is incorrect

    using this the values are miles off,

    is this the correct way to count or is their another domain function that is used in this scenario

    many thanks

    Steve

  2. #2
    ranman256's Avatar
    ranman256 is offline VIP
    Windows Vista Access 2010 32bit
    Join Date
    Apr 2014
    Location
    Kentucky
    Posts
    9,525
    the 'OR' means what can appear in the 1 field: BOB or SAM (the field can be BOB or it can be SAM)

    'AND' is when you want to have 2 different fields [name] = BOB AND [state] = 'NY' (all Bobs who live in NY)

  3. #3
    sdel_nevo is offline Competent Performer
    Windows 7 32bit Access 2010 32bit
    Join Date
    Apr 2013
    Location
    Gloucester, UK
    Posts
    402
    Hi Ranman256

    wow that was quick

    in the code the itemnumber has only been planned once, but two others orders with the same part number are running and one has been completed

    Nz(DCount("ItemNumber", "UNEXData", "ItemNumber = forms.UNEXdetails.ITEMNUMBER And Planned = true or Running =true or Completed =true"), 0) I would have thought that I would get the value back as 4

    1 planned and 2 running and 1 completed I can't seem to get my head around why the value is so far out.

    Steve

  4. #4
    ranman256's Avatar
    ranman256 is offline VIP
    Windows Vista Access 2010 32bit
    Join Date
    Apr 2014
    Location
    Kentucky
    Posts
    9,525
    also, (not sure about your example but)
    you cant use "Planned = true or Running =true " as a string, TRUE is numeric , so dont put it inside the quotes.

    If this string is not the case,
    you may be wanting to ask 2 different querys planned = true
    then
    ask running = true

  5. #5
    sdel_nevo is offline Competent Performer
    Windows 7 32bit Access 2010 32bit
    Join Date
    Apr 2013
    Location
    Gloucester, UK
    Posts
    402
    Hi Ranman256

    many thanks for the heads up, can't believe I missed that, I will ask the two separate queries

    many thanks again

    steve

  6. #6
    John_G is offline VIP
    Windows 7 32bit Access 2010 32bit
    Join Date
    Oct 2011
    Location
    Ottawa, ON (area)
    Posts
    2,615
    Another problem you are facing is the concept of operator precedence when it comes to AND and OR. The way you have it in your post #3, you are counting records where Running =true OR Completed =true REGARDLESS of the value of ItemNumber on the form. Is this what you really want?

    If what you want is to count records for a specific ItemNumber where any of Planned, Running or Completed is true, then you need brackets:

    Nz(DCount("ItemNumber", "UNEXData", "ItemNumber = forms.UNEXdetails.ITEMNUMBER And (Planned = true or Running =true or Completed =true") ), 0)

    You might also want to take the form field reference out of the quotation marks:

    Nz(DCount("ItemNumber", "UNEXData", "ItemNumber = " & forms.UNEXdetails.ITEMNUMBER & " And (Planned = true or Running =true or Completed =true") ), 0)

  7. #7
    sdel_nevo is offline Competent Performer
    Windows 7 32bit Access 2010 32bit
    Join Date
    Apr 2013
    Location
    Gloucester, UK
    Posts
    402
    Hi John G
    Many thanks for he reply, I will try the solution you have posted first thing in the morning when I'm back in the office.

    I think this forum is fantastic it's so nice to get help and advise from such helpful people, I have really enjoyed learning vba but it seams the more I think I know the less I actually do lol,

    Once again many thanks
    Steve

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

Similar Threads

  1. Replies: 1
    Last Post: 09-03-2014, 03:27 AM
  2. Replies: 8
    Last Post: 07-15-2014, 05:56 PM
  3. Replies: 2
    Last Post: 11-14-2012, 04:47 PM
  4. Replies: 11
    Last Post: 03-29-2012, 02:32 PM
  5. Replies: 16
    Last Post: 07-22-2011, 09:23 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