Visual Basic Help

minendo

Elite Member
Aug 31, 2001
35,560
22
81
Ok so I have my loop and the rest of my program working just fine, but I dont know how to incorporate a certain line or even what code to put in. What I need is for the number of years it took for the principle to double to be displayed in a label. I have attached the code. Can someone please help me with setting up code so that I can tell how many years it takes for the principle to double? thanks in advance

For n = 1 To years Step 1 'loop used to calculate yearly compounded dividends

d = Rate * Principle 'Calculates dividends

Principle = Principle + d ' calculates total principle per year

totald = totald + d ' calculates total dividends earned over time

txtOutput.Text = txtOutput.Text & rightalign(n, cfYearwidth) & rightalign(Format(d, "$0.00"), cfDividendwidth) & rightalign(Format(Principle, "$0.00"), cfBal) & rightalign(Format(totald, "$0.00"), cfAccum) & vbCrLf

Next n
 

NikPreviousAcct

No Lifer
Aug 15, 2000
52,763
1
0
Okay, I haven't worked with VB for a while, but I'll try giving it my best shot. I worked with VB 6.0, so if a new version is out and it's got some different syntax, forgive me.

From what I understand, you have a bank account program that compounds interest and you're looking for a script to add to the final code to tell you how long it will take to double, right?

Hmm...


Try something simple like each time that interest is compounded (yearly? ...or every 12 times if it's monthly) you can have a seperate variable that's something like "YearsToDouble" and you just add the little bit into the loop that compounds interest to add one to it each time the year cycles. To do this cleanly, you'll probably want two principle variables. Principle and Principle1

Have your program check to see if "Principle * 2 <= PrinciplePlusInterest". If the final number (PrinciplePlusInterest) isn't more than double of the initial principle (Principle), go through the loop again and tack on some code similar to "YearsToDouble = YearsToDouble + 1"

and, when the loop finally doubles Principle, label the "YearsToDouble" and you'll have how many it took to do it :)

Hope that helps. If not, clerify?

Nik
 

minendo

Elite Member
Aug 31, 2001
35,560
22
81
that would work but what my problem is: the user enters Initial Principle, Rate, and Years. I have to display information for every year and the amount of time it takes to double. here is a pic of what it should look like

Pic
 

NikPreviousAcct

No Lifer
Aug 15, 2000
52,763
1
0
That'll be a bit more difficult. Nice layout, btw. :)

You can have the user put in all that information, have the program save those bits to the variables when "Calculate" is clicked, show all the pertinent information, etc.

BUT


If you have a variable for each and every year so you can display each year individually, you run into the problem of not knowing how many the user will need and run the risk of not making enough variables or have too many just sitting around doing nothing.

With your txtOutput.Text script, you won't need to worry about having enough labels either, which is nice. A suggestion might be putting a script into the loop that will amend the results of each year into the text file displayed before going on to make the next year's calculations. Are you familiar with amending? That way, you get each year displayed with the pertinent information, then the calculation goes on to display the other years as it goes along. Yes, it's displaying as it is working, but it should be so fast that you won't notice much unless you have some ungodly number of years to display.

Help at all?

Nik
P.S. I might be misunderstanding how your txtOutput.Text script works. Me go look again...