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

selecting data by exact date or the next older one

Hi,

need some help.

Got table A with two columns: type DATE, VALUE
Got table B with two columns: type DATE, VALUE

Now the thing is to update table A with the values from table B.
For each element in A I check if there is a corresponding element
(DATE as reference) in B.
If so, I update VALUE of A with VALUE from B. If not, I update VALUE
of A with the next older element of B.

How do we do this?
Thanks so long

Apr 2 '07 #1
4 1352
tone wrote:
Hi,

need some help.

Got table A with two columns: type DATE, VALUE
Got table B with two columns: type DATE, VALUE

Now the thing is to update table A with the values from table B.
For each element in A I check if there is a corresponding element
(DATE as reference) in B.
If so, I update VALUE of A with VALUE from B. If not, I update VALUE
of A with the next older element of B.

How do we do this?
Thanks so long
You could write a function similar to below and substituting the
table/field names with yours. In the value column, in the Update row,
you'd call it with
GetTheValue([Date])

The SQL would look similar to
UPDATE Table1 SET Table1.Value = GetTheValue([Year]);

Public Function GetTheValue(datF As Date) As Variant
Dim rst As Recordset
Dim var As Variant

Set rst = CurrentDb.OpenRecordset("Table2", dbOpenSnapshot)
rst.FindFirst "Year = #" & datF & "#"
If Not rst.NoMatch Then
'it found the date
GetTheValue = rst!Value
Else
'see if a lesser date exists
var = DMax("Year", "Table2", "Year < #" & datF & "#")
If Not IsNull(var) Then
'looks for next lesser date
rst.FindFirst "Year = #" & var & "#"
GetTheValue = rst!Value
End If
End If

rst.Close
Set rst = Nothing

End Function
Apr 2 '07 #2
On 2 Apr., 22:36, salad <o...@vinegar.comwrote:
tonewrote:
Hi,
need some help.
Got table A with two columns: typeDATE, VALUE
Got table B with two columns: typeDATE, VALUE
Now the thing is to update table A with the values from table B.
For each element in A I check if there is a corresponding element
(DATEas reference) in B.
If so, I update VALUE of A with VALUE from B. If not, I update VALUE
of A with the nextolderelement of B.
How do we do this?
Thanks so long

You could write a function similar to below and substituting the
table/field names with yours. In the value column, in the Update row,
you'd call it with
GetTheValue([Date])

The SQL would look similar to
UPDATE Table1 SET Table1.Value = GetTheValue([Year]);

Public Function GetTheValue(datF AsDate) As Variant
Dim rst As Recordset
Dim var As Variant

Set rst = CurrentDb.OpenRecordset("Table2", dbOpenSnapshot)
rst.FindFirst "Year = #" & datF & "#"
If Not rst.NoMatch Then
'it found thedate
GetTheValue = rst!Value
Else
'see if a lesserdateexists
var = DMax("Year", "Table2", "Year < #" & datF & "#")
If Not IsNull(var) Then
'looks for next lesserdate
rst.FindFirst "Year = #" & var & "#"
GetTheValue = rst!Value
End If
End If

rst.Close
Set rst = Nothing

End Function- Zitierten Text ausblenden -

- Zitierten Text anzeigen -
salad, this kinda makes sence to me.
Anyway I have a hard time since the debugger bugs me at 'rst.FindFirst
"Year=#" & datF & "#" '.
It says syntax-error 3077.
Later on also where we concatenate the date.

Other question: "Value" and "Year" are supposed to be the two columns
of Table2?
Later

Apr 3 '07 #3
tone wrote:
On 2 Apr., 22:36, salad <o...@vinegar.comwrote:
>>tonewrote:
>>>Hi,
>>>need some help.
>>>Got table A with two columns: typeDATE, VALUE
Got table B with two columns: typeDATE, VALUE
>>>Now the thing is to update table A with the values from table B.
For each element in A I check if there is a corresponding element
(DATEas reference) in B.
If so, I update VALUE of A with VALUE from B. If not, I update VALUE
of A with the nextolderelement of B.
>>>How do we do this?
Thanks so long

You could write a function similar to below and substituting the
table/field names with yours. In the value column, in the Update row,
you'd call it with
GetTheValue([Date])

The SQL would look similar to
UPDATE Table1 SET Table1.Value = GetTheValue([Year]);

Public Function GetTheValue(datF AsDate) As Variant
Dim rst As Recordset
Dim var As Variant

Set rst = CurrentDb.OpenRecordset("Table2", dbOpenSnapshot)
rst.FindFirst "Year = #" & datF & "#"
If Not rst.NoMatch Then
'it found thedate
GetTheValue = rst!Value
Else
'see if a lesserdateexists
var = DMax("Year", "Table2", "Year < #" & datF & "#")
If Not IsNull(var) Then
'looks for next lesserdate
rst.FindFirst "Year = #" & var & "#"
GetTheValue = rst!Value
End If
End If

rst.Close
Set rst = Nothing

End Function- Zitierten Text ausblenden -

- Zitierten Text anzeigen -


salad, this kinda makes sence to me.
Anyway I have a hard time since the debugger bugs me at 'rst.FindFirst
"Year=#" & datF & "#" '.
It says syntax-error 3077.
Later on also where we concatenate the date.

Other question: "Value" and "Year" are supposed to be the two columns
of Table2?
Later
K. Value and Year are the field names you supplied. I wouldn't use
them but perhaps they're the name you are using. You must change those
names to match your field name....hmmm I see you have typeDate and I
used Date.

Next,
Dim rst As Recordset
might need to be changed to
Dim rst As DAO.Recordset
as your reference might be pointing first to ADO.

Make sure you change the table names unless it really is Table1 and Table2.

The code above worked for me in the query I created.

Apr 3 '07 #4
Thank you salad - so long.
I tried it with DAO but without success.
Might be I didn't investigate much enough to get right into it.
For now I am off. Then in like two months it will become serious
again.
Later
Apr 12 '07 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

21
by: Batista, Facundo | last post by:
Here I send it. Suggestions and all kinds of recomendations are more than welcomed. If it all goes ok, it'll be a PEP when I finish writing/modifying the code. Thank you. .. Facundo
2
by: C.F. Scheidecker Antunes | last post by:
Hello all, I wonder if is there any way to select from a table all the records which has a Date Field that is at least five minutes old? In other words, I have a table with a date field and I...
3
by: Justin Emlay | last post by:
I'm hopping someone can help me out on a payroll project I need to implement. To start we are dealing with payroll periods. So we are dealing with an exact 10 days (Monday - Friday, 2 weeks). ...
1
by: Andrew Arace | last post by:
I scoured the groups for some hands on code to perform the menial task of exporting table data from an Access 2000 database to Oracle database (in this case, it was oracle 8i but i'm assuming this...
2
by: James Radke | last post by:
Hello, I have a vb.net windows application that is calling an older DLL provided by a third party. They supplied a VB 6 application that, when run on my systemn successfully passes data to the...
5
by: pt | last post by:
Hi, i am wonderng what is faster according to accessing speed to read these data structure from the disk in c/c++ including alignment handling if we access it on little endian system 32 bits...
3
by: thisisjim | last post by:
I have a table with a number of fields, one being a date. What I'm looking to do is take a date variable, and then select only one row - that being the one that contains the next date value in...
4
by: J | last post by:
I am editing a pre-existing view. This view is already bringing data from 40+ tables so I am to modify it without screwing with anything else that is already in there. I need to (left) join it...
3
by: HSXWillH | last post by:
I've looked through the site and not found what I'm looking for here. I am not code-versed or anything like that so my skills are rudimentary at best. I'm using Access 03 on a Windows Vista...
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: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
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: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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...
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.