Solved

Listbox Value Not Registering for Calculation

Posted on 2013-05-13
17
343 Views
Last Modified: 2013-05-13
I am trying to build a mortgage calculator application with VB and I have list box to select the number of years, but it does not seem to be actually factoring into the solution.  
This is the code:
Private Sub calcbutton_Click(sender As System.Object, e As System.EventArgs) Handles calcbutton.Click
        'The car price should appear in terms of dollars and cents.
        Dim carprice As Double
        'The APR needs two percentage points
        Dim interestrate As Double
        'Money down should be dollars and cents
        Dim moneydown As Double
        'Dim loan term As Double
        Dim loanterm As Double
        'Dim Payment as Double
        Dim payment As Double
        Dim monthlyinterest As Double
        Dim months As Double
        Dim loanamount As Double

        'Declare where the values are coming from
        carprice = Val(Carpricetext.Text)
        interestrate = Val(IRTextbox.Text)
        moneydown = Val(Dwnpymnttext.Text)
        loanterm = Val(LnTrmList.SelectedValue)
        loanamount = (carprice - moneydown)






        Do While loanterm <= 5
            monthlyinterest = (interestrate / 100) / 12
            months = 12 * loanterm

            'calculate payments - rate * month = monthly payments
            payment = _
                Pmt(monthlyinterest, months, -loanamount)


            'displays results
            paymenttxt.Text = payment


        Loop
    End Sub

Open in new window

       loanterm = Val(LnTrmList.SelectedValue) should tell me the value that is selected, but that part of the equation is just coming up zero.  Any ideas on what I did wrong?
0
Comment
Question by:aclaus225
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 7
  • 6
  • 4
17 Comments
 
LVL 10

Expert Comment

by:CSIPComputing
ID: 39162126
Shouldn't LnTrmList.SelectedValue be LnTrmList.SelectedItem.Value?

I'm not in front of a box with VB on it, but I think that's where you're slightly awry.

Use your trace environment and watches to keep an eye on LnTrnList.SelectedItem.Value to check, or use a MsgBox to display the value...
0
 

Author Comment

by:aclaus225
ID: 39162166
MsgBox after the calculation showed that Months was registering as 0.
0
 
LVL 86

Accepted Solution

by:
Mike Tomlinson earned 250 total points
ID: 39162185
You should be using SelectedItem(), not SelectedValue().  Also, you don't need the loop:
        'Declare where the values are coming from
        carprice = Val(Carpricetext.Text)
        interestrate = Val(IRTextbox.Text)
        moneydown = Val(Dwnpymnttext.Text)
        loanterm = Val(LnTrmList.SelectedItem)

        loanamount = (carprice - moneydown)
        monthlyinterest = (interestrate / 100) / 12
        months = 12 * loanterm

        'calculate payments - rate * month = monthly payments
        payment = Pmt(monthlyinterest, months, -loanamount)

        'displays results
        paymenttxt.Text = payment

Open in new window

0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:aclaus225
ID: 39162237
This is what I have:
        'The car price should appear in terms of dollars and cents.
        Dim carprice As Double
        'The APR needs two percentage points
        Dim interestrate As Double
        'Money down should be dollars and cents
        Dim moneydown As Double
        'Dim loan term As Double
        Dim loanterm As Double
        'Dim Payment as Double
        Dim payment As Double
        Dim monthlyinterest As Double
        Dim months As Double
        Dim loanamount As Double

        'Declare where the values are coming from
        carprice = Val(Carpricetext.Text)
        interestrate = Val(IRTextbox.Text)
        moneydown = Val(Dwnpymnttext.Text)
        loanterm = Val(LnTrmList.SelectedItem)

        loanamount = (carprice - moneydown)
        monthlyinterest = (interestrate / 100) / 12
        months = 12 * loanterm

        'calculate payments - rate * month = monthly payments
        payment = Pmt(monthlyinterest, months, -loanamount)

        'displays results
        paymenttxt.Text = payment
    End Sub

Open in new window


And it still does not seem to do the calculation.  
It will not do the payment = line as "Argument 'NPer' is not a valid value."
0
 
LVL 86

Expert Comment

by:Mike Tomlinson
ID: 39162246
What kind of values do you have in your ListBox?  I put in years, such as: 3, 4, 5 ,6 , 7, etc...
0
 

Author Comment

by:aclaus225
ID: 39162261
The values in the listbox are:
1
2
3
4
5
0
 
LVL 10

Expert Comment

by:CSIPComputing
ID: 39162265
NPer is the number of months in the PMT calculation.

If your number of months is still showing as "0" then your calculation won't work.

I get the error "Argument 'NPer' is not a valid value." if I do this calculation:

Payment = Pmt(0.25, 0, 2000)

are you using SelectedItem or SelectedItem.value?  Does it make a difference?
0
 
LVL 86

Expert Comment

by:Mike Tomlinson
ID: 39162290
After the changes, it worked perfectly for me.  =\

You could also make sure there is an item selected in the ListBox:

    If LnTrmList.SelectedIndex <> -1 Then
        loanterm = Val(LnTrmList.SelectedItem)

        loanamount = (carprice - moneydown)
        monthlyinterest = (interestrate / 100) / 12
        months = 12 * loanterm

        Debug.Print("loanterm = " & loanterm)
        Debug.Print("months = " & months)

        'calculate payments - rate * month = monthly payments
        payment = Pmt(monthlyinterest, months, -loanamount)

        'displays results
        paymenttxt.Text = payment        
    Else
        MessageBox.Show("Please select a Term!")
    End If

With the Debug.Prints() in there, what do you get in the Immediate window?
0
 

Author Comment

by:aclaus225
ID: 39162321
Predictably, I get: Please Select a Term.  I have no other code on this form.
This is all the code on this page:

Option Strict On

Public Class childform



    Private Sub calcbutton_Click(sender As System.Object, e As System.EventArgs) Handles calcbutton.Click
        'The car price should appear in terms of dollars and cents.
        Dim carprice As Double
        'The APR needs two percentage points
        Dim interestrate As Double
        'Money down should be dollars and cents
        Dim moneydown As Double
        'Dim loan term As Double
        Dim loanterm As Double
        'Dim Payment as Double
        Dim payment As Double
        Dim monthlyinterest As Double
        Dim months As Double
        Dim loanamount As Double

        'Declare where the values are coming from
        carprice = Val(Carpricetext.Text)
        interestrate = Val(IRTextbox.Text)
        moneydown = Val(Dwnpymnttext.Text)
        loanterm = CDbl(LnTrmList.SelectedItem)

        If LnTrmList.SelectedIndex <> -1 Then
            loanterm = Val(LnTrmList.SelectedItem)

            loanamount = (carprice - moneydown)
            monthlyinterest = (interestrate / 100) / 12
            months = 12 * loanterm

            Debug.Print("loanterm = " & loanterm)
            Debug.Print("months = " & months)

            'calculate payments - rate * month = monthly payments
            payment = Pmt(monthlyinterest, months, -loanamount)

            'displays results
            paymenttxt.Text = CStr(payment)
        Else
            MessageBox.Show("Please select a Term!")
        End If


     
    End Sub

End Class
0
 
LVL 10

Expert Comment

by:CSIPComputing
ID: 39162322
I'm now in front of a VB machine as well.

I've recreated the problem quite simply (not bothered naming my textboxes etc).

And this works!

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim Payment As Double
        Dim loanamount As Double = (Val(TextBox1.Text) - Val(TextBox3.Text))
        Dim monthlyinterest As Double = (Val(TextBox2.Text) / 100) / 12
        Dim loanterm = Val(ListBox1.SelectedItem)
        Dim months = 12 * loanterm

        'calculate payments - rate * month = monthly payments
        Payment = Pmt(monthlyinterest, months, -loanamount)

        'displays results
        TextBox4.Text = Payment


    End Sub

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        ListBox1.Items.Add("1")
        ListBox1.Items.Add("2")
        ListBox1.Items.Add("3")
        ListBox1.Items.Add("4")
        ListBox1.Items.Add("5")

    End Sub

Open in new window

0
 
LVL 10

Assisted Solution

by:CSIPComputing
CSIPComputing earned 250 total points
ID: 39162363
If you're getting a "Please select a term", something is wrong with your listbox (you knew this though!).

Does your form look like this? Loan Calculator Screen
0
 

Author Comment

by:aclaus225
ID: 39162407
CSIP,

The only difference between your form and mine is that the listbox is shrunken to only show one value at a time with an arrow.
0
 

Author Comment

by:aclaus225
ID: 39162409
Wow, I hate to admit it, but my mistake with this was simply that I did not select the number (highlight).
0
 
LVL 10

Expert Comment

by:CSIPComputing
ID: 39162414
Is your list box set to allow multiple selections?

If so, you need to read the selected values in a different way.

I'll set my box like that and see if it makes a difference.
0
 
LVL 10

Expert Comment

by:CSIPComputing
ID: 39162420
Doh!

Glad you got to the bottom of it!
0
 
LVL 10

Expert Comment

by:CSIPComputing
ID: 39162430
Fair split.  I'm happy :-)
0
 
LVL 86

Expert Comment

by:Mike Tomlinson
ID: 39162524
Hehe!...did you by chance stay up all night?

Drink some coffee bud.   ;)
0

Featured Post

Secure Your Active Directory - April 20, 2017

Active Directory plays a critical role in your company’s IT infrastructure and keeping it secure in today’s hacker-infested world is a must.
Microsoft published 300+ pages of guidance, but who has the time, money, and resources to implement? Register now to find an easier way.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
VB.NET 2008 - Windows Protected Your PC 7 55
VBA: Insert New column with specific format type 12 55
Very Large data in MYSQL 7 95
FInd Image Control Gridview 3 38
The ECB site provides FX rates for major currencies since its inception in 1999 in the form of an XML feed. The files have the following format (reducted for brevity) (CODE) There are three files available HERE (http://www.ecb.europa.eu/stats/exch…
When trying to find the cause of a problem in VBA or VB6 it's often valuable to know what procedures were executed prior to the error. You can use the Call Stack for that but it is often inadequate because it may show procedures you aren't intereste…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

730 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question