CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 7 of 7
  1. #1
    Join Date
    Apr 2006
    Posts
    587

    validation problem

    Code:
    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!

  2. #2
    Join Date
    Jan 2006
    Location
    Fox Lake, IL
    Posts
    15,007

    Re: validation problem

    Code:
    LoanAmount = txtLoanAmount.Text

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

    Code:
    LoanAmount = txtLoanAmount.ToInt32
    or whatever.
    David

    CodeGuru Article: Bound Controls are Evil-VB6
    2013 Samples: MS CODE Samples

    CodeGuru Reviewer
    2006 Dell CSP
    2006, 2007 & 2008 MVP Visual Basic
    If your question has been answered satisfactorily, and it has been helpful, then, please, Rate this Post!

  3. #3
    Join Date
    Apr 2006
    Posts
    587

    Re: validation problem

    Thanks for the reply, it's still giving me the same error though!

  4. #4
    Join Date
    Jan 2006
    Location
    Fox Lake, IL
    Posts
    15,007

    Re: validation problem

    What line is crashing? Did you change ALL your fields to the proper type?

    Post new code.
    David

    CodeGuru Article: Bound Controls are Evil-VB6
    2013 Samples: MS CODE Samples

    CodeGuru Reviewer
    2006 Dell CSP
    2006, 2007 & 2008 MVP Visual Basic
    If your question has been answered satisfactorily, and it has been helpful, then, please, Rate this Post!

  5. #5
    Join Date
    Apr 2006
    Posts
    587

    Re: validation problem

    Code:
    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

  6. #6
    Join Date
    Jun 2004
    Location
    NH
    Posts
    678

    Re: validation problem

    This should get the types correct, but I haven't looked at the logic at all.

    Code:
            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

  7. #7
    Join Date
    Jul 2008
    Location
    WV
    Posts
    5,362

    Re: validation problem

    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.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured