Results 1 to 10 of 10
  1. #1
    maxchang9 is offline Novice
    Windows 10 Access 2016
    Join Date
    Nov 2018
    Posts
    7

    Multiple criterias in Dlookup function

    First of all, i wrote my access in Chinese, so instead of screenshots ill explain the problem the best i can
    ---------------------------------------------------------------------------
    Table A
    Number Ingredient
    1 A
    2 B
    3 C
    4 D

    TableB
    OrderNumber Import OrderPrice
    1 A 100
    1 B 50
    2 A 500
    2 C 5
    2 D 2000
    3 B 40
    3 D 1000

    (Table A ! [Ingredient]) 1 ~ ∞ (Table B ! [Import] )

    Query only include TableA


    ---------------------------------------------------------------------------------------------------------------------------------
    I want to make a Query indicate the latest OrderPrice (which has the largest OrderNumber) of each Ingredient

    What i expect:

    Number Ingredient LatestPrice
    1 A 500
    2 B 40
    3 C 5
    4 D 1000

    So far i got it right if each criteria run "independently"

    Latestprice: DLookUp("OrderPrice","TableB","OrderNumber=" & DMax("OrderNumber","TableB") & "" ) 'find OrderPrice with largest OrderNumber

    Number Ingredient LatestPrice
    1 A 1000
    2 B 1000
    3 C 1000
    4 D 1000


    Latestprice: DLookUp("OrderPrice","TableB", "[Import]='" & [Ingredient] & "'") 'find OrderPrice with corresponding Ingredient

    Number Ingredient LatestPrice
    1 A 100
    2 B 50
    3 C 5
    4 D 2000


    However, when i combine these as:
    Latestprice: DLookUp("OrderPrice","TableB","OrderNumber=" & DMax("OrderNumber","TableB") & "" And "[Import]='" & [Ingredient] & "'")

    The 1st criteria (Dmax) seems to overwrite the 2nd criteria

    Number Ingredient LatestPrice
    1 A 1000
    2 B 1000
    3 C 1000
    4 D 1000

    So, where did i did wrong?

    Please help me out, and thank you .
    ---------------------------------------------------------------------------------------------------------------------------
    heres the screenshot of my query
    Click image for larger version. 

Name:	未命名.jpg 
Views:	29 
Size:	104.3 KB 
ID:	37104

  2. #2
    Join Date
    Apr 2017
    Posts
    1,681
    Quote Originally Posted by maxchang9 View Post
    Latestprice: DLookUp("OrderPrice","TableB","OrderNumber=" & DMax("OrderNumber","TableB") & "" And "[Import]='" & [Ingredient] & "'")
    Create a saved query
    Code:
    qLatestOrder = SELECT Ingredient, MAX(OrderNumber) As LatestOrder FROM TableB GROUP BY Ingredient;
    Your query will be
    Code:
    SELECT lo.Ingredient, lo.LatestOrder, tb.OrderPrice AS [LatestPrice] FROM qLatestOrder lo LEFT JOIN TableB tb ON lo.LatestOrder = tb.OrderNumber AND lo.Ingredient = tb.Ingredient

  3. #3
    maxchang9 is offline Novice
    Windows 10 Access 2016
    Join Date
    Nov 2018
    Posts
    7
    You mean make query directly from SQL? I tried and it demands me to enter Ingredient parameter value 3 times, in the end i got a table with 3 columns: ingredient + latestOrder + LatestPrice (which doesnt belong to the ingredient).

  4. #4
    Join Date
    Apr 2017
    Posts
    1,681
    ???
    Both queries read data from table TableB, and return latest orders and latest prices for all ingredients.

    Now I see in your post #1, that there is it for some reason instead Ingredient used Import as column name - missed it yesterday! Replace "Ingredient" with "Import" in both query definitions!

  5. #5
    maxchang9 is offline Novice
    Windows 10 Access 2016
    Join Date
    Nov 2018
    Posts
    7
    Big thanks, i finally solve it myself with similar method (2 queries) you suggest. I did it in design view though, since i am not familiar with SQL code. Just curious how does " lo. " and " tb. " works?

  6. #6
    dashiellx's Avatar
    dashiellx is offline Falconer
    Windows 10 Access 2016
    Join Date
    Jan 2019
    Location
    Baltimore
    Posts
    49
    I'm not sure DLookup is the way to go here. I think you might need to create a custom function to do what you want.

    Something along the lines of:

    Code:
    Public Function GetLastPrice(strIngredient as String) as Currency
    Dim strSQL as String
    Dim rstLastPrice As DAO.Recordset
    Dim currTemp as Currency
    
    strSQL = ""
    strSQL = strSQL & "SELECT Max(TableB.OrderNumber), TableB.Import, Last(TableB.OrderPrice)
    strSQL = strSQL & " FROM TableB
    strSQL = strSQL & " WHERE (((TableB.Import)='" & strIngredient & "'))"
    strSQL = strSQL & " GROUP BY TableB.Import;
    set rstLastPrice = currentdb.OpenRecordset(strSQL, dbreadonly)
    If not rstLastPrice.eof and not rstLastPrice.BOF then 
        currTemp = rstLastPrice(0)
    Else
       currTemp = 0
    End if
    GetLastPrice=currTemp 
    End Function
    *Results and capitalization will vary.

  7. #7
    maxchang9 is offline Novice
    Windows 10 Access 2016
    Join Date
    Nov 2018
    Posts
    7
    Thank you very much I finally solved it myself with the method u suggest (2 queries). I have to do it in design view since i am not familiar with SQL code yet. Just curious how does " lo. " and " tb. " works?

  8. #8
    dashiellx's Avatar
    dashiellx is offline Falconer
    Windows 10 Access 2016
    Join Date
    Jan 2019
    Location
    Baltimore
    Posts
    49
    Just curious how does " lo. " and " tb. " works?
    when writing SQL, rather than having to type the full table name all the time, you can use an Alias in the From statement of you query and then use that Alias in the Select, Where, Order and Group By clauses.

  9. #9
    maxchang9 is offline Novice
    Windows 10 Access 2016
    Join Date
    Nov 2018
    Posts
    7
    Indeed, i give up dlookup after hours of attempt, the main problem is because 2nd criteria will eliminate the results in 1st criteria. I also messed up the syntax too, Since OrderNumber is numerical + "" should surround AND, the correct syntax is:
    DLookUp("OrderPrice","TableB","OrderNumber= & DMax("OrderNumber","TableB") And [Import]='" & [Ingredient] & "'")

    Anyway thanks for the reply, ill study your code during weekend

  10. #10
    ssanfu is offline Master of Nothing
    Windows 7 32bit Access 2010 32bit
    Join Date
    Sep 2010
    Location
    Anchorage, Alaska, USA
    Posts
    9,664
    Quote Originally Posted by maxchang9 View Post
    DLookUp("OrderPrice","TableB","OrderNumber= & DMax("OrderNumber","TableB") And [Import]='" & [Ingredient] & "'")
    You are missing a closing double quote after "OrderNumber=" and an amphersand and double quote at the end of the DLookup function
    Code:
    DLookUp("OrderPrice","TableB","OrderNumber= " & DMax("OrderNumber","TableB") & " And [Import]='" & [Ingredient] & "'"

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

Similar Threads

  1. DLOOKUP Function for Multiple Criteria.
    By cap.zadi in forum Forms
    Replies: 9
    Last Post: 05-31-2017, 05:01 PM
  2. How to search for multiple criterias using comboBox?
    By AccessPractice in forum Programming
    Replies: 2
    Last Post: 04-29-2016, 03:59 AM
  3. Replies: 15
    Last Post: 07-09-2015, 01:39 AM
  4. Replies: 5
    Last Post: 06-27-2013, 02:49 PM
  5. Update Multiple fields according multiple criterias
    By denis.m in forum Programming
    Replies: 1
    Last Post: 02-21-2011, 11:03 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