473,386 Members | 1,785 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

Finding values based on dates in a query

Hi Everybody,

I'm having problems getting my head around finding a value (price) based on a date range. I have created a table which lists product items, price, and date (that the price will come into effect). Some products have more than one price and date.

What I would like to do is, select the product, select, for example a random date, and the query will find price that the product is valued at, at that time.

I've been using the IIf function in queries like...

IIF([DateCosted]>#01/02/2009# And [Product]="House",350,IIF([DateCosted]>#02/03/2010# And [Product]="House",450,200)) etc

But now I've reached a wall and have to do it another way

I have super limited knowledge of VBA, so I'm hoping the Grandmasters can possibly guide me to another method.

Cheers Eddie
May 15 '10 #1

✓ answered by NeoPa

I expect you'd need to link your tabe into a grouped subquery that finds the max date which is <= to that specified on your form or entered by the operator, depending on how you do that bit. Something like :
Expand|Select|Wrap|Line Numbers
  1. SELECT   [Product]
  2.        , [Price]
  3.        , [DateCosted]
  4.  
  5. FROM     [tblProduct] AS tP INNER JOIN
  6.     (
  7.     SELECT   [Product]
  8.            , Max([DateCosted]) AS [MaxDate]
  9.     FROM     [tblProduct]
  10.     WHERE    [DateCosted]<=[Enter Date or Get From Form]
  11.     GROUP BY [Product]
  12.     ) AS subQ
  13.   ON     tP.Product=subQ.Product
  14.  AND     tP.DateCosted=subQ.MaxDate

5 2098
NeoPa
32,556 Expert Mod 16PB
You say VBA rather than query. What are you hoping to do with the returned value?
May 15 '10 #2
Hi Neo,

I'm wanting to use the value returned for calculations in another query, ultimately to populate a form and report

Cheers Eddie
May 15 '10 #3
NeoPa
32,556 Expert Mod 16PB
I expect you'd need to link your tabe into a grouped subquery that finds the max date which is <= to that specified on your form or entered by the operator, depending on how you do that bit. Something like :
Expand|Select|Wrap|Line Numbers
  1. SELECT   [Product]
  2.        , [Price]
  3.        , [DateCosted]
  4.  
  5. FROM     [tblProduct] AS tP INNER JOIN
  6.     (
  7.     SELECT   [Product]
  8.            , Max([DateCosted]) AS [MaxDate]
  9.     FROM     [tblProduct]
  10.     WHERE    [DateCosted]<=[Enter Date or Get From Form]
  11.     GROUP BY [Product]
  12.     ) AS subQ
  13.   ON     tP.Product=subQ.Product
  14.  AND     tP.DateCosted=subQ.MaxDate
May 15 '10 #4
Thanks so much Neo,

I think setting the date costed as max date is the piece of logic Im missing

Cheers Eddie
May 15 '10 #5
NeoPa
32,556 Expert Mod 16PB
A pleasure. An interesting question :)
May 16 '10 #6

Sign in to post your reply or Sign up for a free account.

Similar topics

3
by: Richard Williamson | last post by:
Hi all, I have managed, using a quite tortuous route, to select certain records in a UNION query. This is based on the results of other queries. Question: how the devil do I change the value...
2
by: RBohannon | last post by:
I have a report with most fields populated by a query. However, some of the fields are variable in such a way that their values cannot be queried from a table. At present the values for these...
19
by: bdt513 | last post by:
I am trying to extract the values from a query using VBA. Specifically, I want to concatenate all the values of the "rosEmail" field from query "qselRosterEmailList" into one string (strEmails). I...
3
by: Steve Weixel | last post by:
I'm having problems getting dates to format the way that I need them to. The problem is that I'm used to the VB6 way of doing things, with which the statement Format(37866, "dd MMM yyyy") would...
0
by: Jeff Boes | last post by:
I hope this helps someone else ... I had struggled some time ago with attempts to get a rank of values query to work, but then I gave up and set it aside. I had another reason to attack it, and in...
3
by: racquetballguy | last post by:
Hi I wish to have a user enter a parameter into a form for the number of rows to use to do an average. This form is based upon a query. SELECT TOP does not support parameters. Something like...
1
by: starke1120 | last post by:
Is there a way to open a form based on query type.. Example.. If a certain query result is 1 then open the form to this result.. If the query results are NULL or 0 results, then open open for...
0
VbaNewbee
by: VbaNewbee | last post by:
I have a form with a few filters. Once the user clicks "search button", the code first evaluates my filters, then shows the query results in a List Box" titled backschedule. I have a few text...
1
by: billypit | last post by:
Hi, In my project i have one table production.Now i have to make application in which i have to insert new data in table by fields of form made in access.I don't know how to use form field's values...
7
by: LSGKelly | last post by:
I am working on a query that I would like to get the top 4 values based on a group. Here is the SQL: SELECT qRenPercRept1.CountOfccName, qRenPercRept1.Merged, qRenPercRept1.ccName,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.