# Help Figuring Out Calculating Payment with VB

I am trying to program a mortgage type calculator with Visual Basic and can't quite figure out 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``````
When I debug this:
payment = _
Pmt(monthlyinterest, months, -loanamount)
This tells me Argument 'NPer' is not a valid value.

Any ideas on what I need to do?
funwithdotnet

It looks like somewhere along the line, months ends up = 0.
Just in case, you might try:
``loanterm = CDbl(LnTrmList.SelectedValue)``

You can also break on line 34, look at the values of the arguments and backtrack from there.
NPer is the second argument in the Pmt function.  It's complaining it doesn't like the value for month.

Add the following line after months = 12 * loanterm to see what is actually being set for months.  You may have to use the CInt function to cast it to an integer.
``MsgBox(months)``