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

How do a copy a field value from a previous record based on criteria?

7
In Access 2002, I have created a Make Table query to add a new field “Base$” through a custom function, moving average, that averages each 8 weeks of data. However, I also need to calculate a different result if there is an entry “X” in the Flag field--where there is a Flag "X", I want to duplicate the 8-week average number of the previous record. What is the VBA code to provide that alternate result? Here is a sample of the data, followed by the custom function.
ID WkEnd ItemNbr Sales$ Flag
464 9/3/05 Item1 22397
527 9/10/05 Item1 21742
591 10/22/05 Item1 22392
649 9/17/05 Item1 21581
715 9/24/05 Item1 21747
780 10/1/05 Item1 21424 X
845 10/8/05 Item1 21477
910 10/15/05 Item1 23806
984 10/29/05 Item1 22927
1049 11/5/05 Item1 33252 X
1114 11/12/05 Item1 20768
1179 11/19/05 Item1 20247
1447 12/17/05 Item1 26336
1513 12/24/05 Item1 32947
1583 12/31/05 Item1 20829
1651 9/3/05 Item2 23097
1719 9/10/05 Item2 44363 X
1835 10/22/05 Item2 38518
1902 9/17/05 Item2 32232
1970 9/24/05 Item2 32468
2036 10/1/05 Item2 41633 X
2105 10/8/05 Item2 27651
2174 10/15/05 Item2 26819

Expand|Select|Wrap|Line Numbers
  1. Function MovAvg(ItemNbr, startDate, period As Integer)
  2.  
  3.     Dim rst As DAO.Recordset
  4.     Dim sql As String
  5.     Dim ma As Currency
  6.     Dim n As Integer
  7.  
  8.     sql = "Select * from tblSalesData "
  9.     sql = sql & "where ItemNbr = '" & ItemNbr & "'"
  10.     sql = sql & " and WkEnd <= #" & startDate & "#"
  11.     sql = sql & " order by WkEnd"
  12.  
  13.     Set rst = CurrentDb.OpenRecordset(sql)
  14.     rst.MoveLast
  15.         For n = 0 To period - 1
  16.         If rst.BOF Then
  17.             MovAvg = 0
  18.             Exit Function
  19.         Else
  20.             ma = ma + rst.Fields("Sales$")
  21.         End If
  22.         rst.MovePrevious
  23.     Next n
  24.     rst.Close
  25.     MovAvg = ma / period
  26.  
  27. End Function
Feb 22 '08 #1
9 1580
VBWheaties
145 100+
Not 100% sure what you need but I think you should use a variable that gets updated with the value of the field you need to look at in hindsight.
Then, when you see your 'x' data, you'll have the variable from the previous record stored.
Feb 25 '08 #2
SuzK
7
Not 100% sure what you need but I think you should use a variable that gets updated with the value of the field you need to look at in hindsight.
Then, when you see your 'x' data, you'll have the variable from the previous record stored.
Thank you for your reply. Your assumption is correct. I'm not very adapt at VBA coding. Can you provide the code to accomplish your suggestion? I'm trying to learn VBA on my own, but I seem to have a mental block on the steps involved in setting variables and then manipulating them.
Feb 25 '08 #3
VBWheaties
145 100+
Thank you for your reply. Your assumption is correct. I'm not very adapt at VBA coding. Can you provide the code to accomplish your suggestion? I'm trying to learn VBA on my own, but I seem to have a mental block on the steps involved in setting variables and then manipulating them.
Dimension a variable (done once at the beginning of your sub)
Expand|Select|Wrap|Line Numbers
  1. Dim myVar As String
  2.  
Then, use it to store you value:
Expand|Select|Wrap|Line Numbers
  1.    myVar = rs("Field1").Value & ""
  2.  
Then, you would check for the X value. If it exists, you'll have previous records value. So make sure you check for X before giving the variable the latest value.
Feb 25 '08 #4
SuzK
7
Dimension a variable (done once at the beginning of your sub)
Expand|Select|Wrap|Line Numbers
  1. Dim myVar As String
  2.  
Then, use it to store you value:
Expand|Select|Wrap|Line Numbers
  1.    myVar = rs("Field1").Value & ""
  2.  
Then, you would check for the X value. If it exists, you'll have previous records value. So make sure you check for X before giving the variable the latest value.
Thanks again for your help and patience.... but I'm still need help with coding an "IF" scenario. If you'll look at my desired results for the "Base$", you'll see that if the value of "promo" is null, I want to calculate Base$ as a moving 8-week average. However, if the value of "promo" is "X", I want to copy the previous record's value for Base$. See an example of my expected results for Base$, particularly for weeks ending 17 Sep through 01 Oct.

ItemNbr Week Ending Sales $ Base$ Promo
10025 16-Jul-05 11349 0
10025 23-Jul-05 13593 0
10025 30-Jul-05 12709 0
10025 06-Aug-05 10104 0
10025 13-Aug-05 8693 0
10025 20-Aug-05 8712 0
10025 27-Aug-05 8532 0
10025 03-Sep-05 9553 10406
10025 10-Sep-05 9713 10201
10025 17-Sep-05 14827 10355
10025 24-Sep-05 13938 10355 X
10025 01-Oct-05 14102 10355 X
10025 08-Oct-05 11052 11304

I can accomplish my goals by using an Iif statement in an Access Make Table query to get the Base$ for "non-promo" weeks and then running a Update query with Dlookup to retrieve the previous record Base$, but I was hoping to accomplish this in one VBA function -- moving average -- as indicated in my initial post.
Feb 25 '08 #5
SuzK
7
Dimension a variable (done once at the beginning of your sub)
Expand|Select|Wrap|Line Numbers
  1. Dim myVar As String
  2.  
Then, use it to store you value:
Expand|Select|Wrap|Line Numbers
  1.    myVar = rs("Field1").Value & ""
  2.  
Then, you would check for the X value. If it exists, you'll have previous records value. So make sure you check for X before giving the variable the latest value.
How do I "check for the X value"?
Feb 27 '08 #6
VBWheaties
145 100+
How do I "check for the X value"?
What I meant by the X variable was to persist (save) the value you needed to perform the calculation when coming across X on the next iteration.
It seems that your logic is based on a backwards read of a subset of records and not on the entire table. Since there are a gazillion ways of doing things, your best bet is to focus on your own logic and take it from there.
Feb 27 '08 #7
SuzK
7
What I meant by the X variable was to persist (save) the value you needed to perform the calculation when coming across X on the next iteration.
It seems that your logic is based on a backwards read of a subset of records and not on the entire table. Since there are a gazillion ways of doing things, your best bet is to focus on your own logic and take it from there.
Thanks, VBWheaties for your patience. My logic is "based on a backwards read of a subset of records". My VBA coding skills are extremely limited. I have the calculations working in Excel where each subgroup is on a separate worksheet, but I need to move to Access because of the increasing number of worksheets.

I have created a working moving average custom function in Access (referenced earlier) which I use in a Make Table query, which works fine. However, I'm having problem updating the values, when the function returns a zero value. These are my needs --

1 - Moving Average function -- Calculates Weekly 8-week moving average for each of 80+ items in my database.
2 - When an item is "on promotion", indicated by an "X" value in the promotion field, I want to repeat the Moving Average value for (1) that item and (2) in the previous week.

I have the above working in an Excel formula, but because of the size of the database, I need to a way to do the same in Access.

Thanks, again. If I don't hear back, I'll assume you've given up on me, and I'll start over elsewhere.
Feb 27 '08 #8
VBWheaties
145 100+
Thanks, VBWheaties for your patience. My logic is "based on a backwards read of a subset of records". My VBA coding skills are extremely limited. I have the calculations working in Excel where each subgroup is on a separate worksheet, but I need to move to Access because of the increasing number of worksheets.

I have created a working moving average custom function in Access (referenced earlier) which I use in a Make Table query, which works fine. However, I'm having problem updating the values, when the function returns a zero value. These are my needs --

1 - Moving Average function -- Calculates Weekly 8-week moving average for each of 80+ items in my database.
2 - When an item is "on promotion", indicated by an "X" value in the promotion field, I want to repeat the Moving Average value for (1) that item and (2) in the previous week.

I have the above working in an Excel formula, but because of the size of the database, I need to a way to do the same in Access.

Thanks, again. If I don't hear back, I'll assume you've given up on me, and I'll start over elsewhere.
I haven't given up, I just don't want to dilute the logic with mine that may cause more confusion.

For the average, when you have your values in Access, you can get the average like so:

Expand|Select|Wrap|Line Numbers
  1.    SELECT AVG(NumberField) AS 8WeekAverage 
  2.    FROM MyTable
  3.    WHERE MyItem = 'ThisItem'
  4.  
If you wanted to show averages for all items:
Expand|Select|Wrap|Line Numbers
  1.    SELECT AVG(NumberField) AS 8WeekAverage, ItemNumber 
  2.    FROM MyTable
  3.    GROUP BY ItemNumber
  4.  
If that doesn't do it, post again so others can have a go at it.
Feb 27 '08 #9
SuzK
7
I haven't given up, I just don't want to dilute the logic with mine that may cause more confusion.

For the average, when you have your values in Access, you can get the average like so:

Expand|Select|Wrap|Line Numbers
  1.    SELECT AVG(NumberField) AS 8WeekAverage 
  2.    FROM MyTable
  3.    WHERE MyItem = 'ThisItem'
  4.  
If you wanted to show averages for all items:
Expand|Select|Wrap|Line Numbers
  1.    SELECT AVG(NumberField) AS 8WeekAverage, ItemNumber 
  2.    FROM MyTable
  3.    GROUP BY ItemNumber
  4.  
If that doesn't do it, post again so others can have a go at it.
Thanks... I already have the averages for all items. My problem is not in calculating the averages -- it is updating the table to copy the previous item weekly average when the week is a promo week. A promo week is identified in my table's field "Promo", where "Promo" = "X". In my Access query, I calculated the weekly averages where Promo <> "X".

Again, thanks for your help. I'll work tonight on how to clarify my question and repost.
Feb 27 '08 #10

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

Similar topics

8
by: mark | last post by:
Access2000 How do I write a query that combines the CTC field from each record below into one record? I need to concatenate the CTC field with a separator, like below: ...
4
by: Gene | last post by:
When entering a record in a form, I would like a value in a field of the previous record to be entered automatically into a different field of the current record. Which way should I go? Is it also...
5
by: Daniel Tan | last post by:
Are there anyway to copy rows of records from one query to another query and then hide the records in source query ? Pls advise. Thanks. Regards, Daniel
25
by: Lyn | last post by:
Hi, I am working on a genealogy form. The only table (so far) lists everybody in the family, one record per person. Each record has an autonum ID. The parent form (frmMainForm) displays the...
1
by: Bruce | last post by:
What is the most efficient (ie easiest for me) way to copy a record from an existing table, appending it to that same table but incrementing a field's value? (code is fine) Cheers very muchly...
3
by: Chris | last post by:
Before I started to create table, etc to track unique form field record number assigments I thought I'd check to see if there is now a better way to do this in .NET. I have a parent form (table)...
2
by: Brett | last post by:
My database has 2 tables: Table1 & Table2. If a field is not null on a record in table2, then the not null fields in table1 that correspond to the records in table1 needs to be updated to match the...
2
by: Swinky | last post by:
I hope someone can help...I feel like I'm walking in the dark without a flashlight (I'm NOT a programmer but have been called to task to do some work in Access that is above my head). I have...
1
by: roveagh1 | last post by:
Hi I've been using the 2 year old link below to repeat values from previous record field into current corresponding field. It's worked fine for text but the last piece of advice was to use the same...
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...
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
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...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

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.