Click to See Complete Forum and Search --> : validation problem


pouncer
February 10th, 2009, 02:32 PM
Dim LoanAmount As Decimal
Dim MonthlyPayment As Decimal
Dim IntrestRate As Decimal
Dim Counter As Integer
Dim Balance As Decimal

lblMsg.Text = ""

If IsNumeric(txtLoanAmount.Text) Then
If txtLoanAmount.Text > 0 Then
If IsNumeric(txtMonthlyRePayment.Text) Then
If txtMonthlyRePayment.Text > 0 Then


LoanAmount = txtLoanAmount.Text
MonthlyPayment = txtMonthlyRePayment.Text
IntrestRate = cboIntrestRate.Text
Balance = LoanAmount

Counter = 1

Do While Balance >= 0
Balance = LoanAmount - MonthlyPayment + (LoanAmount * IntrestRate)

LoanAmount = Balance
If Balance >= 0 Then
lstPaymentSchedule.Items.Add("Month " & Counter & " Balance is £" & Math.Round(Balance, 2))
Else
lstPaymentSchedule.Items.Add("Month " & Counter & " Balance is £0.00")

End If
Counter += 1
Loop

Else
lblMsg.Text = "Please enter a value bigger then 0"
End If
Else
lblMsg.Text = "Please enter a valid Monthly Repaymment"
End If

Else
lblMsg.Text = "Please enter a value greater than 0"
End If

Else
lblMsg.Text = "Please enter a valid Loanamount"
End If


End Sub


Then interface is like
Loan amount : ______ (user puts in data)
Monthly repay: ______ (user puts in data)
Intrest rate: ______ (user selects 0.001, 0.002. 0.003. 0.004)

Payment sched:
Month 1 balance =
month 2 balance =

etc

Everything works fine

just got a problem with few amounts e.g

If user puts in

Loan amount: 10000
Month repy: 1
intrest rate: 0.001

program crashes and says

Value was either too large or too small for a Decimal. (Pointing at the Monthly repayment.

ow can i get round dis

Thanks in advance!

dglienna
February 10th, 2009, 06:40 PM
LoanAmount = txtLoanAmount.Text



You are using a STRING value instead of what you need.

LoanAmount = txtLoanAmount.ToInt32


or whatever.

pouncer
February 10th, 2009, 07:14 PM
Thanks for the reply, it's still giving me the same error though!

dglienna
February 10th, 2009, 07:23 PM
What line is crashing? Did you change ALL your fields to the proper type?

Post new code.

pouncer
February 10th, 2009, 08:50 PM
Dim LoanAmount As Decimal
Dim MonthlyPayment As Decimal
Dim IntrestRate As Decimal
Dim Counter As Integer
Dim Balance As Decimal

lblMsg.Text = ""

If IsNumeric(txtLoanAmount.Text) Then
If txtLoanAmount.Text > 0 Then
If IsNumeric(txtMonthlyRePayment.Text) Then
If txtMonthlyRePayment.Text > 0 Then


LoanAmount = txtLoanAmount.Text.ToInt32
MonthlyPayment = txtMonthlyRePayment.Text.ToInt32
IntrestRate = cboIntrestRate.Text.ToInt32
Balance = LoanAmount

Counter = 1

Do While Balance >= 0
Balance = LoanAmount - MonthlyPayment + (LoanAmount * IntrestRate)

LoanAmount = Balance
If Balance >= 0 Then
lstPaymentSchedule.Items.Add("Month " & Counter & " Balance is £" & Math.Round(Balance, 2))
Else
lstPaymentSchedule.Items.Add("Month " & Counter & " Balance is £0.00")

End If
Counter += 1
Loop

Else
lblMsg.Text = "Please enter a value bigger then 0"
End If
Else
lblMsg.Text = "Please enter a valid Monthly Repaymment"
End If

Else
lblMsg.Text = "Please enter a value greater than 0"
End If

Else
lblMsg.Text = "Please enter a valid Loanamount"
End If


End Sub

TT(n)
February 10th, 2009, 10:05 PM
This should get the types correct, but I haven't looked at the logic at all.


Dim LoanAmount As Decimal
Dim MonthlyPayment As Decimal
Dim IntrestRate As Decimal
Dim Balance As Decimal
Dim Counter As Integer = 1
lblMsg.Text = ""
If IsNumeric(txtLoanAmount.Text) Then
If CDec(txtLoanAmount.Text) > 0 Then
If IsNumeric(txtMonthlyRePayment.Text) Then
If CDec(txtMonthlyRePayment.Text) > 0 Then
LoanAmount = CDec(txtLoanAmount.Text)
MonthlyPayment = CDec(txtMonthlyRePayment.Text)
IntrestRate = CDec(cboIntrestRate.Text)
Balance = LoanAmount
Counter = 1
Do While Balance >= 0
Balance = LoanAmount - MonthlyPayment + (LoanAmount * IntrestRate)
LoanAmount = Balance
If Balance >= 0 Then
lstPaymentSchedule.Items.Add("Month " & Counter & " Balance is £" & Math.Round(Balance, 2))
Else
lstPaymentSchedule.Items.Add("Month " & Counter & " Balance is £0.00")
End If
Counter += 1
Loop
Else
lblMsg.Text = "Please enter a value bigger then 0"
End If
Else
lblMsg.Text = "Please enter a valid Monthly Repaymment"
End If
Else
lblMsg.Text = "Please enter a value greater than 0"
End If
Else
lblMsg.Text = "Please enter a valid Loanamount"
End If

DataMiser
February 10th, 2009, 10:35 PM
Loan amount: 10000
Month repy: 1
intrest rate: 0.001

it appears to me that your interest in this case would be greater than the payment so the number would grow until it crashes the program. Payment should never be less than the interest due at any given time.