Results 1 to 6 of 6
  1. #1
    klnlsu is offline Competent Performer
    Windows 7 64bit Access 2013
    Join Date
    Apr 2015
    Posts
    107

    getting NULL value when trying to use DLOOKUP in VBA code on an aggregate query

    I have created an aggregate query to calculate the mode (or most occurrences) of a discount_pct value in a selection of data. I originally had my criteria set in my query in which it compared the OrderHeaderID to a textbox value on an open form. My SQL code for this query is as follows:



    Code:
    SELECT TOP 1 tblOEOrderDetail.discount_pct
    FROM tblOEOrderDetail
    GROUP BY tblOEOrderDetail.discount_pct, tblOEOrderDetail.OrderHeaderID
    HAVING (((tblOEOrderDetail.OrderHeaderID)=[forms]![frmOrderHeaderEntry]![OrderHeaderIDTxt]))
    ORDER BY Count(tblOEOrderDetail.discount_pct) DESC;
    I then stored a value in a recordset field by using this query in a dlookup statement in my VBA code as follows:

    Code:
    rsOEORDHDR("discount_pct") = DLookup("discount_pct", "qryModeOfDiscountPct")
    This worked.

    I now don't have a form field to base my criteria value on, so I removed the criteria from the query itself. The SQL code for my revised query is as follows:

    Code:
    SELECT TOP 1 tblOEOrderDetail.discount_pct, tblOEOrderDetail.OrderHeaderID
    FROM tblOEOrderDetail
    GROUP BY tblOEOrderDetail.discount_pct, tblOEOrderDetail.OrderHeaderID
    ORDER BY Count(tblOEOrderDetail.discount_pct) DESC;
    I then tried to pass the criteria from a variable (ProcessOrderID, which is numeric) in my VBA subroutine to the query. My revised dlookup statement is as follows:

    Code:
    rsOEORDHDR("discount_pct") = DLookup("discount_pct", "qryModeOfDiscountPct", "OrderHeaderID = " & ProcessOrderID)
    I am getting a NULL value in my recordset field. I can set the criteria in the query to the value that the variable field should be passing to it and I get the correct query value, so I think the problem is my dlookup statement.

    Can I pass criteria to an aggregate query this way? If so, what am I doing wrong?

    Thanks.

  2. #2
    aytee111 is offline Competent At Times
    Windows 10 Access 2013 64bit
    Join Date
    Nov 2011
    Location
    Nomad
    Posts
    3,936
    Have you done a debug thru this code and hovered over ProcessOrderID to confirm that it has the correct value?

  3. #3
    klnlsu is offline Competent Performer
    Windows 7 64bit Access 2013
    Join Date
    Apr 2015
    Posts
    107
    Quote Originally Posted by aytee111 View Post
    Have you done a debug thru this code and hovered over ProcessOrderID to confirm that it has the correct value?
    Yes, I have.

  4. #4
    aytee111 is offline Competent At Times
    Windows 10 Access 2013 64bit
    Join Date
    Nov 2011
    Location
    Nomad
    Posts
    3,936
    It all looks ok. Try changing the dlookup to read direct from the table, see if that works.
    DLookup("Max(discount_pct)","tblOEOrderDetail","Or derHeaderID = " & ProcessOrderID)

  5. #5
    Micron is online now Very Inert Person
    Windows 7 32bit Access 2007
    Join Date
    Jun 2014
    Location
    Ontario, Canada
    Posts
    13,422
    Code:
    SELECT TOP 1 tblOEOrderDetail.discount_pct, tblOEOrderDetail.OrderHeaderID
    FROM tblOEOrderDetail
    GROUP BY tblOEOrderDetail.discount_pct, tblOEOrderDetail.OrderHeaderID
    ORDER BY Count(tblOEOrderDetail.discount_pct) DESC;
    If you dump this into a new query, does it return any records? If not, that should explain it. But, it looks like your criteria was one thing on a certain field
    [forms]![frmOrderHeaderEntry]![OrderHeaderIDTxt] but now you are trying to pass a number to a different field
    "OrderHeaderID = " & ProcessOrderID

    The more we hear silence, the more we begin to think about our value in this universe.
    Paraphrase of Professor Brian Cox.

  6. #6
    klnlsu is offline Competent Performer
    Windows 7 64bit Access 2013
    Join Date
    Apr 2015
    Posts
    107
    I think I have figured it out...Apparently Access runs the query first and then applies the filter I passed to it. I had to remove the SELECT TOP 1 from my query and then call it with DFIRST instead of DLOOKUP.

    Updated SQL for query:

    Code:
    SELECT tblOEOrderDetail.OrderHeaderID, tblOEOrderDetail.discount_pct, Count(tblOEOrderDetail.discount_pct) AS CountOfdiscount_pct
    FROM tblOEOrderDetail
    GROUP BY tblOEOrderDetail.OrderHeaderID, tblOEOrderDetail.discount_pct
    ORDER BY Count(tblOEOrderDetail.discount_pct) DESC;
    Updated call from VBA:

    Code:
    rsOEORDHDR("discount_pct") = DFirst("discount_pct", "qryModeOfDiscountPct", "OrderHeaderID = " & ProcessOrderID)
    This seems to be working.

    Thanks for y'all's help!

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

Similar Threads

  1. Replies: 10
    Last Post: 05-01-2017, 06:11 AM
  2. Replies: 8
    Last Post: 09-12-2015, 11:28 AM
  3. Access dlookup invalid use of null
    By cuddihy1990 in forum Programming
    Replies: 4
    Last Post: 06-25-2014, 03:38 PM
  4. Handling Null DLookup result
    By j6sander in forum Access
    Replies: 1
    Last Post: 07-19-2012, 11:07 AM
  5. Dlookup() for NULL
    By jgelpi16 in forum Programming
    Replies: 3
    Last Post: 03-31-2011, 12:37 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