Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

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

Status
Not open for further replies.

StevenFromSouth

Programmer
Jul 31, 2021
12
0
0
TT
Hello, new member here.
I'm not too good when it comes to queries but 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

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?
 
You could try create a current prices query with SQL like:
SQL:
SELECT UPC, 
  (SELECT TOP 1 Price
   FROM [Prices Table] P
   WHERE P.UPC = [Products table].UPC AND EffDate <= Date()
   ORDER BY EffDate DESC) as CurrentPrice
FROM [Products table]

Duane
Minnesota
Hook'D on Access
MS Access MVP 2001-2016
 
Also, welcome to Tek-Tips. It's a good practice to mark a reply as "Great Post" when your question has been successfully answered.


Duane
Minnesota
Hook'D on Access
MS Access MVP 2001-2016
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top