473,569 Members | 2,836 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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

7 New Member
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 1582
VBWheaties
145 New Member
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 New Member
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 New Member
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 New Member
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 New Member
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 New Member
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 New Member
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 New Member
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 New Member
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
7083
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: BattID VehicleID STDATE STTIME CTC LKO500HF 00000000 10/27/2003 4:13:51 AM 4 LKO500HF 00000000 10/27/2003 5:13:51 AM 5 LKO500HF 00000000 10/27/2003 10:13:51 AM 6...
4
12818
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 possible to do this based on criteria from the previous record?
5
2665
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
10183
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 data in each record, which includes the ID of the father and the mother (who also have records in the table). One record per form. I have a Tab...
1
1473
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 Bruce
3
2577
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) and children form (table). Relationship equals one to many. I'd like to auto number the fields accordingly and traditionaly I assign a unique...
2
19576
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 field in table2. What I have is a form that is linked to Table2. If the users want to change a field in the main database (table1), they fill the...
2
3453
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 code that will successfully copy a record and append the information to a new record in the same table (parent table) within a form. However, there...
1
4329
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 logic for a date field. i.e. theValue.defaultvalue = "#" & theValue.value & "#" I can't get this to work for some reason and I can't figure out...
0
7612
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7922
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
8119
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7668
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
6281
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
0
3637
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2111
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1209
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
936
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.