Results 1 to 6 of 6
  1. #1
    AdeshM is offline Novice
    Windows 10 Access 2016
    Join Date
    Jul 2021
    Posts
    3

    How to join tables to get current price of a product.

    Hello, new member here.



    Still learning queries and how to join tables because I wanted to stop using DLookups, I've read that they can be highly inefficient and unstable. I saw some posts on this forums and Allen Browne have emphasized that joined queries and subqueries are far superior.

    I have a Products table
    UPC, ItemName, Measurement

    Prices Table
    UPC, EffDate, Quantity, Price, Discount

    I am using this function to get the Current Price for a given Product

    HTML Code:
    Public Function GetPrice(inpUPC As Double, inpQuantity As Double, inpDate As Date) As Double
    
    On Error Resume Next
    
    Dim strPrice As Double
    Dim strRegPrice As Double
    Dim strDiscount As Double
    Dim strMaxDate As Date
    
    inpDate = Format(inpDate, "m/d/yyyy")
    
    strMaxDate = DMax("EFFDATE", "Prices", "UPC= " & inpUPC & " AND EFFDATE<= # " & inpDate & " # And Quantity= " & inpQuantity & " ")
    
    strPrice = DLookup("Price", "Prices", "UPC= " & inpUPC & " And EFFDATE= #" & strMaxDate & " # And Quantity= " & inpQuantity & " ")
    
    strRegPrice = DLookup("Price", "Prices", "UPC= " & inpUPC & " And EFFDATE= #" & strMaxDate & " # And Quantity= 1 ")
    
    If GetDiscount(inpUPC, inpQuantity, inpDate) = 0 Then
        GetPrice = strRegPrice
    Else
        GetPrice = strPrice
    End If
    
    End Function
    Can anyone guide me to converting this to a query?

  2. #2
    June7's Avatar
    June7 is online now VIP
    Windows 10 Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    50,292
    Domain aggregate functions in query and on forms can cause slow performance. I have an app that uses at least a hundred DLookup() in VBA and never had an issue. If you don't use DLookup(), will have to open recordset object.

    UPC is a number field?

    Don't need to join those two tables to get price info.

    Query like:

    SELECT Prices.*
    FROM Prices INNER JOIN (SELECT UPC, Max(EffDate) AS MED FROM Prices WHERE EffDate <= [enter date] GROUP BY UPC) AS MaxD
    ON (Prices.EffDate = MaxD.MED) AND (Prices.UPC = MaxD.UPC)
    WHERE Prices.UPC=[enter upc];

    Provide sample data - can build tables in post.

    Procedure like:
    Code:
    Function GetPrice(inpUPC, inpQty, inpDate)
    Dim rs As DAO.Recordset
    Set rs = CurrentDb.OpenRecordset("SELECT Prices.* FROM Prices " & _
             "INNER JOIN (SELECT UPC, Max(EffDate) AS MED FROM Prices WHERE EffDate <=#" & inpDate & "# GROUP BY UPC) AS MaxD " & _
              "ON (Prices.EffDate = MaxD.MED) AND (Prices.UPC = MaxD.UPC) " & _
             "WHERE Prices.UPC = " & inpUPC)
    rs.FindFirst "Quantity = " & inpQty
    If inpQty = 1 Or rs!Discount <> 0 Then
        GetPrice = rs!Price
    Else
        rs.FindFirst "Quantity = 1"
        GetPrice = rs!Price
    End If
    End Function
    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.

  3. #3
    AdeshM is offline Novice
    Windows 10 Access 2016
    Join Date
    Jul 2021
    Posts
    3
    Thank you. This is some data from my prices table, I will try your code and see how it goes.


    UPC EFFDATE PRICE QUANTITY DISCOUNT VAT
    689784873309 1/1/2019 2.00 1.00 0.00 No
    689784873309 1/1/2019 1.00 24.00 18.00 No
    689784873309 1/1/2021 0.83 120.00 20.00 No


    Update: It is only returning 0.83 as the price no matter what quantity is used.

    It should be
    1 to 23 units = $2.00
    24 units = $1.00
    120 units = $0.83

  4. #4
    June7's Avatar
    June7 is online now VIP
    Windows 10 Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    50,292
    So if order date is 7/24/2020 and quantity is 25 to 120, price would be 1.00?
    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.

  5. #5
    Join Date
    Jan 2017
    Location
    Swansea,South Wales,UK
    Posts
    1,195
    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?search_query=debug+access+vba



  6. #6
    AdeshM is offline Novice
    Windows 10 Access 2016
    Join Date
    Jul 2021
    Posts
    3
    June7, I think I may need to restructure my Products Table to separate Case/Boxed/Pack Items to have their own UPC Code. There are too many variables to account for if I wanted to have fixed prices based on Quantity.

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

Similar Threads

  1. Price List Structure - Numerous Attributes for Each Product
    By Sunny8760 in forum Database Design
    Replies: 12
    Last Post: 06-21-2018, 12:20 AM
  2. Price Calculation base upon product age
    By streub in forum Access
    Replies: 6
    Last Post: 11-13-2017, 09:31 AM
  3. Last purchase price of a product sale
    By EMAS in forum Queries
    Replies: 4
    Last Post: 12-22-2015, 02:01 PM
  4. Replies: 4
    Last Post: 04-26-2013, 08:32 AM
  5. Replies: 1
    Last Post: 10-06-2009, 02:00 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