Results 1 to 5 of 5
  1. #1
    Khalil Handal is offline Competent Performer
    Windows 10 Access 2010 32bit
    Join Date
    Jun 2016
    Posts
    310

    DCount with two criteria

    Hi to all,

    I have this line of VBA code and is working well.
    Code:
    Me.txtLoanCount = DCount("*", "tblBorrowedBooks", "[fkBookId] = " & [pkBookId])
    I want to add a second criteria for a date field to be null (DateHoldRequest Is Null) using AND operator.
    I am not able to do it. Any suggestions please?

    I tried this code


    Code:
    DCount("*", "tblBorrowedBooks", "[fkBookId]='" & [pkBookId] & "' AND [DateHoldRequest] is null")
    But had an error: 3464 data type mismatch in criteria expression
    Output: 2 AND [DateHoldRequest] is null)

    Any ideas?
    Khalil

  2. #2
    Join Date
    Jan 2017
    Location
    Swansea,South Wales,UK
    Posts
    6,570
    You went and put single quotes around a numeric value, when it did not need it in the first place, as shown by your previous working code.
    Single quotes are for strings.

    Strings need to be surrounded by single quotes ' unless it contains a single quote, then triple double quotes works, I think?
    Date literals with # and in mm/dd/yyyy or yyyy-mm-dd format
    Numbers do not need anything

    I tend to put the criteria into a string variable for domain functions if not a single simple criteria.
    Then I can debug.print them to see if I have the syntax correct. You might want to start doing that as well.

    Code:
    DCount("*", "tblBorrowedBooks", "[fkBookId]= & [pkBookId] & " AND [DateHoldRequest] is Null")
    Please use # icon on toolbar when posting code snippets.
    Cross Posting: https://www.excelguru.ca/content.php?184
    Debugging Access: https://www.youtube.com/results?sear...bug+access+vba

  3. #3
    Khalil Handal is offline Competent Performer
    Windows 10 Access 2010 32bit
    Join Date
    Jun 2016
    Posts
    310
    Hi,
    Thank you for the reply and for information given.
    In your code, the criteria shows only THREE double quotes; Should it be Four ?
    It shows the line in red color in the VBA window.

  4. #4
    Join Date
    Jan 2017
    Location
    Swansea,South Wales,UK
    Posts
    6,570
    Yes, put one after the = sign.
    Please use # icon on toolbar when posting code snippets.
    Cross Posting: https://www.excelguru.ca/content.php?184
    Debugging Access: https://www.youtube.com/results?sear...bug+access+vba

  5. #5
    Khalil Handal is offline Competent Performer
    Windows 10 Access 2010 32bit
    Join Date
    Jun 2016
    Posts
    310
    Thank you very much.
    Solved

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

Similar Threads

  1. dcount with 2 criteria
    By Nobby2193 in forum Access
    Replies: 2
    Last Post: 02-04-2019, 03:46 AM
  2. dcount criteria
    By markjkubicki in forum Programming
    Replies: 2
    Last Post: 11-26-2018, 03:01 PM
  3. DCount converse criteria
    By DubCap01 in forum Forms
    Replies: 5
    Last Post: 05-25-2015, 04:13 PM
  4. Using DCount, how to use a variable as the criteria
    By crowegreg in forum Programming
    Replies: 2
    Last Post: 02-17-2015, 05:18 PM
  5. DCount with Multiple Criteria
    By Newbie11 in forum Reports
    Replies: 4
    Last Post: 05-25-2012, 09:04 AM

Tags for this Thread

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