473,378 Members | 1,622 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,378 software developers and data experts.

vlookup for date?

aas4mis
97
I have a form with a FSC field that has a percentage value. I'm wanting to fill that value with data from a "lookup" table. I'm aware of how to get this done in excell but am having problems with access. I've been searching for a while and come across querydef's which have only confused me more. My lookup table has dates in one field and values such as ".1", ".12", ".15" in the other.

I found an example of:
Expand|Select|Wrap|Line Numbers
  1. Set qdf = CurrentDb.CreateQueryDef("", "SELECT T1.Increment FROM Table1 AS T1 WHERE T1.Value1=(SELECT Min(T2.Value1) FROM Table1 AS T2 WHERE T2.Value1>=[Value];);")
I can get this to work with double data type, but can't figure out how to lookup by date. Any help would be appreciated.
Mar 21 '08 #1
3 2517
janders468
112 Expert 100+
What's in your lookup table are you wanting to look up a percent by date?
Mar 21 '08 #2
aas4mis
97
My lookup table is two fields, Date and Percentage. Percentage could be written as decimal. .1 for 10% etc.., Yes. I would like to return a percentage by date range.
Mar 24 '08 #3
aas4mis
97
I found a code snippet from Aaron Ringer that solved my problem. It was basically the same I had but in a different layout. It was easier to debug with his code.

Expand|Select|Wrap|Line Numbers
  1. Public Function AccessVLookup(strTable As String, strLookupField As String, _
  2.           varLookupValue As Variant, strReturnField As String, _
  3.           Optional strCriteriaField As String, Optional varCriteriaValue As Variant) As Variant
  4.       'Aaron Ringer 26 Feb 08
  5.       'Simulates the Excel VLookup function in Access, complete with Range Lookup argument.
  6.       'I've used it to look up currency exchange rates, to get the XRate active at the given date.
  7.       'It means that there doesn't have to be an entry on the given date, it'll find the next lowest value.
  8.       'Can be used to look up anything, as long as the lookup field contains numeric data.
  9.       'Returns zero if nothing found.
  10.       'strTable = Name of lookup table.
  11.       'strLookupField = Name of field to search.
  12.       'varLookupValue = Value to look for in lookup field.
  13.       'strReturnField = Field to return value from.
  14.       'Example:
  15.       'Gets the exchange rate for the given currency active as of today.
  16.       'dblExchangeRate = AccessVLookup("tblCurrencyExchangeRate", "EffectiveDate", Date, "ExchangeRate", "CurrencyID", lngCurrencyID)
  17.  
  18.         Dim strSQL As String
  19.         Dim qdf As DAO.QueryDef
  20.         Dim rst As DAO.Recordset
  21.  
  22.         strSQL = "SELECT "
  23.         strSQL = strSQL & "T1." & strReturnField
  24.         strSQL = strSQL & " FROM " & strTable & " AS T1 "
  25.         strSQL = strSQL & "WHERE T1." & strLookupField & "="
  26.         strSQL = strSQL & "(SELECT Max(T2." & strLookupField & ") "
  27.         strSQL = strSQL & "FROM " & strTable & " AS T2 "
  28.         strSQL = strSQL & "WHERE T2." & strLookupField & " <= "
  29.         strSQL = strSQL & "[LookupValue]"
  30.  
  31.         If Len(strCriteriaField) > 0 Then
  32.           strSQL = strSQL & " AND [" & strCriteriaField & "]"
  33.           strSQL = strSQL & " = " & varCriteriaValue
  34.         End If
  35.  
  36.         strSQL = strSQL & ")"
  37.  
  38.         Set qdf = CurrentDb.CreateQueryDef("", strSQL)
  39.         qdf.Parameters("LookupValue") = varLookupValue
  40.  
  41.         Set rst = qdf.OpenRecordset
  42.         If rst.RecordCount > 0 Then AccessVLookup = rst.Fields(strReturnField)
  43.  
  44.         rst.Close
  45.         qdf.Close
  46.  
  47.         Set rst = Nothing
  48.         Set qdf = Nothing
  49.  
  50.       End Function
Another issue I had with my previous code wasn't in the code at all. It was where I was getting my date from. Access won't let you assign a value from a disabled field. I had to copy the field and make it invisible in order to use it. Hopefully this helps somebody.
Mar 24 '08 #4

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

Similar topics

2
by: chudson007 | last post by:
I've got a data set on which I'm trying to perform the same as excel's vlookup formula using SQL, but do not know how. Can anyone help? If you picture me having two tables Table_A and Table_B and...
3
by: Fred | last post by:
I am trying to come up with a way to lookup values in Access as easily as I can in Excel. My application is looking up scores. An example lookup table below explains what I'm after: TABLE1...
3
by: Fred | last post by:
I have an application that involves calculating a score based on a number of defects. Simplified example of the scoring: No_Defects Score 1-3 A 4-15 ...
2
by: Rebecca | last post by:
could someone tell me the correct syntax for using vlookup in visual basic
1
by: Crash91 | last post by:
I am using this formula in my sheet =VLOOKUP(A10,table,2) A10 is a cell that has the function :Left(sheet1!"c10"), it contains the correct value needed for the lookup but its isnt working! Can...
5
by: catlover30 | last post by:
HI, I need help with excel VLOOKUP! I am at a new job and really trying to solve this without asking anyone at work, since they seem th expect me to know this. What I am trying to acheive is the...
4
by: ritheshtitu1982 | last post by:
Hi, I would like to perform Vlookup in access. Eg. I have two Tables Code table Code --- No { ------- 0 ...
1
by: scubasteve | last post by:
Looking up values from an Access table is simple. Simulating the 'Range Lookup' functionality from Excel's VLookup formula is a bit trickier. For those that aren't familiar with this, it allows...
9
by: Sinner | last post by:
Hi, I have a field name 'USER' in tableMAIN. How do I replace the user names with corresponding user names. I can do that in xl using vlookup but now I'm trying to find a way to do that in...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
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
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
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.