×
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Contact US

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

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

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

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

(OP)
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 --> vba

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?

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

You could try create a current prices query with SQL like:

CODE --> 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

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

(OP)
Thank you that worked.

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

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

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members! Already a Member? Login

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close