473,769 Members | 1,748 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Adding New Table Records and calculating a Percentage increase

17 New Member
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 tblBudgetShareP roj.

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 frmBudgetShareP roj 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. rcdBudgetShareP roj.Year(I) or something.

Thanks so much for your help in advance.

Zoe
Aug 3 '08 #1
4 4846
hjozinovic
167 New Member
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,573 Recognized Expert Moderator MVP
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 rcdBudgetShareP roj![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 New Member
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 tblBudgetShareP roj, I could then perform on the previous record.

i.e.

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

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

Set rcdBudgetShareP roj = CurrentDb.OpenR ecordset("tblBu dgetShareProjec tions")

MaxID = Queries!qryMaxI D![MaxID]

With rcdBudgetShareP roj
For I = 1 To 3
Call .AddNew
![Year] = intYear + 1
![InflationEstima te] = 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 New Member
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
5290
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 (table.ID is TestTable) On .aspx page: <HeaderTemplate> <asp:Table ID="TestTable" runat="Server" /> </HeaderTemplate>
6
2413
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 abc 2 abc 3
25
5196
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 calculate the totals in each row. when i try however, i receive the error: "Error: 'elements' is null or not an object"
3
6905
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 columns using a combo box within a form. These initials are stored in an engineer table in which the combo
22
18813
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. The system is client/server, multiusers based. The MDBs are using record locking. Here is part of the code: Dim wkSpace As Workspace, db As Database Dim rstTrans As DAO.Recordset Set wkSpace = DBEngine.Workspaces(0)
8
2390
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 sp_spaceused on it. The index_size was also pretty big in 6 digits. On looking at the tableA
135
4321
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
1734
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 table A has 2.3 M records. Child table B has 200,000 records. - The parent table has 5 indexes. Child table A has 6 indexes and Child table B has one index. - Both child tables has contraints with 'on delete cascade' parameter.
4
3116
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 calculate as such in that formula n display in the price1 text using the text property onblur,
0
9423
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 synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10212
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. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10047
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 captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9863
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8872
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 launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7410
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5447
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3962
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
2
3563
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.