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

Adding New Table Records and calculating a Percentage increase

17
Hi.

I have a form which the user enters 2 years worth of data into (one record per year). The aim, is to populate the table this form is based on with 3 more years worth of data (i.e. creating 3 new records), based on a percentage increase on the previous year. This form is based directly on a table called tblBudgetShareProj.

So far I have the following code but I am COMPLETELY new to VB and I'm very aware of how incomplete it is. I have a few ideas but no idea how to implement them.

So far when frmBudgetShareProj closes, it opens another form called frmPercent. In this form is an unbound text box called "Percent" which I would like the user to enter the percentage increase in, which will remain constant for the next 3 years. There are then 2 buttons, one to close (which works) and one called cmdCalculate, which on press, should add 3 new records and calculate the percentage increase for each record based on the previous.

I thought that perhaps selecting maximum user ID and then performing calculations based on the data for the fields associated with that ID would enable me to do the calculation based on the previous record. But then I didn't know how to control only doing this 3 times.

Any help would be GREATLY appreciated.
Expand|Select|Wrap|Line Numbers
  1. Private Sub cmdCalculate_Click()
  2. Dim dbBudget As DAO.Database
  3. Dim rcdBudgetShareProj As DAO.Recordset
  4. Dim Percent As Variant
  5. Dim I As Integer
  6.  
  7. Set dbBudget = CurrentDb
  8. Set rcdBudgetShareProj = dbBudget.OpenRecordset("tblBudgetShareProj")
  9. Set Percent = frmPercent![Percent]
  10.  
  11. For I = 1 To 3
  12. rcdBudgetShareProj.AddNew
  13. rcdBudgetShareProj![Year] = rcdBudgetShareProj![Year] + 1
  14. rcdBudgetShareProj![InflationEstimate] = Percent
  15. rcdBudgetShareProj![Budget/Estimate] = "Estimate"
  16. rcdBudgetShareProj![Statemented] = rcdBudgetShareProj![Statemented] * (Percent + 1)
  17. rcdBudgetShareProj.Update
  18. Next I
  19.  
  20. End Sub
Currently, setting Percent says the value is missing so I think I've reference that unbound text box wrong.

And I know the for loop isn't stepping through anything but I didn't know how to incorporate the I value into the fields i.e. rcdBudgetShareProj.Year(I) or something.

Thanks so much for your help in advance.

Zoe
Aug 3 '08 #1
4 4824
hjozinovic
167 100+
Try seting Percent variable by referencing forms, like:
Expand|Select|Wrap|Line Numbers
  1. Set Percent = Forms!frmPercent![Percent]
And the loop can be:
Expand|Select|Wrap|Line Numbers
  1. i = 0
  2. Do Until i = 3
  3. rcdBudgetShareProj.AddNew
  4. rcdBudgetShareProj![Year] = rcdBudgetShareProj![Year] + 1
  5. rcdBudgetShareProj![InflationEstimate] = Percent
  6. rcdBudgetShareProj![Budget/Estimate] = "Estimate"
  7. rcdBudgetShareProj![Statemented] = rcdBudgetShareProj![Statemented] * (Percent + 1)
  8. rcdBudgetShareProj.Update
  9. i = i + 1
  10. Loop
Aug 7 '08 #2
NeoPa
32,556 Expert Mod 16PB
I suspect the Percent problem is related to trying to treat the value as an object.

VBA is very forgiving about how you define variables, which often means that errors are easier to introduce. I suspect you really want Percent to be a Double (double precision) variable. It should then be set, in case of an empty value, using Nz(). To simplify later calculations though, we also add 1 at this time.
Expand|Select|Wrap|Line Numbers
  1. Dim Percent As Double
  2. Percent = Nz(frmPercent.Percent, 0)
Your Year code cannot use rcdBudgetShareProj![Year], as that is indeterminate after moving away from the current record (part of your processing). This must be available somewhere from elsewhere than here - either on a form or in your code. The same is true for the [Statemented] value.

You're right to use the For Next construct for your loop. This is the most appropriate of the looping constructs to use. I would however suggest using With for your main object (The recordset).

As near as possible, your code should be like the following :
Expand|Select|Wrap|Line Numbers
  1. Private Sub cmdCalculate_Click()
  2.   Dim rcdBudgetShareProj As DAO.Recordset
  3.   Dim Percent As Double
  4.   Dim I As Integer
  5.   Dim intYear As Integer
  6.   Dim curStatemented As Currency
  7.  
  8.   'Handle the operator not entering a value for Percent better than this
  9.   '... unless no increase is what you want as a default
  10.   Percent = 1 + Nz(frmPercent![Percent], 0)
  11.   'Arrange to populate these or use a control on a form
  12.   intYear = ...
  13.   curStatemented = ...
  14.   Set rcdBudgetShareProj = CurrentDb.OpenRecordset("tblBudgetShareProj")
  15.  
  16.   With rcdBudgetShareProj
  17.     For I = 1 To 3
  18.       Call .AddNew
  19.       ![Year] = intYear + 1
  20.       ![InflationEstimate] = Percent
  21.       ![Budget/Estimate] = "Estimate"
  22.       ![Statemented] = curStatemented * (Percent ^ I)
  23.       Call .Update
  24.     Next I
  25.   End With
  26.  
  27. End Sub
From your original code I've guessed that the value you are expecting for a value of 27 percent would be 27 percent itself (or 0.27). My code works on this assumption.

Let us know how you get on :)
Aug 10 '08 #3
zoeb
17
Hi, thanks so much for all your help, it's so frustrating when you don't quite know enough to get to the final solution.

Just one more quick question,

I was looking to update on the previous record, so I thought by having a query finding the maximum ID in the tblBudgetShareProj, I could then perform on the previous record.

i.e.

Private Sub cmdUpdate_Click()
Dim rcdBudgetShareProj As DAO.RecordsetD
Dim Percent As Double
Dim I As Integer
Dim MaxID As Integer

Percent = 1 + Nz(frmPercent![Percent], 0)

Set rcdBudgetShareProj = CurrentDb.OpenRecordset("tblBudgetShareProjections ")

MaxID = Queries!qryMaxID![MaxID]

With rcdBudgetShareProj
For I = 1 To 3
Call .AddNew
![Year] = intYear + 1
![InflationEstimate] = Percent
![Budget/Estimate] = "Estimate"
![Statemented] = ![Statemented](MaxID) * (Percent ^ I)
Call .Update
Next I
End With

End Sub

I don't know if my syntax is correct but I hope you get what I'm trying to explain.

Thanks once again and sorry for the very late response.
Aug 19 '08 #4
zoeb
17
P.S. Having thought about this, doing it this way, should MaxID actually be MaxID -1, because when I add a new record, the ID count will increase and to query on the previous record I would need to refer to one less than the item calculated in the query.

Thanks,

Zoe
Aug 19 '08 #5

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

Similar topics

5
by: Sue | last post by:
On code-behind page: (attributes set programatically for each of these elements) linkbutton added to tablecell textbox added to tablecell tablecells added to tablerow tablerow added to table...
6
by: Jamie Fryatt | last post by:
Hi everyone, here's what id like to do. I have a table with 2 fields, name and value I need to be able to add multiple records quickly, for example I need to add name value abc 1...
25
by: kie | last post by:
hello, i have a table that creates and deletes rows dynamically using createElement, appendChild, removeChild. when i have added the required amount of rows and input my data, i would like to...
3
by: JDiamond | last post by:
Hi, I have a table called Hosts. The Hosts table contains the following fields: Each field represents a step in the project. The tech that completes each step initials the respective...
22
by: RayPower | last post by:
I'm having problem with using DAO recordset to append record into a table and subsequent code to update other tables in a transaction. The MDB is Access 2000 with the latest service pack of JET 4....
8
by: rshivaraman | last post by:
Hi : I have a TableA with around 10 columns with varchar and numeric datatypes It has 500 million records and its size is 999999999 KB. i believe it is kb i got this data after running...
135
by: robinsiebler | last post by:
I've never had any call to use floating point numbers and now that I want to, I can't! *** Python 2.5.1 (r251:54863, May 1 2007, 17:47:05) on win32. *** 0.29999999999999999 0.29999999999999999
1
by: karpalmera | last post by:
Hello! We are trying to delete 1,500 records from a parent table with two child tables and it is taking hours to execute. Here is the set-up. - The parent table has 800,000 records. Child...
4
by: sureshl | last post by:
function cal() { var f = document.form1; var regExp_Count = new RegExp("^+$"); f.price1.value = parseFloat(f.baseprice.value*(f.percen.value/100)).toFixed(0); } cal() functions , will...
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: 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
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
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.