Results 1 to 9 of 9

Thread: Assistance with MsgBox in Program

  1. #1

    Thread Starter
    New Member
    Join Date
    Jul 2007
    Posts
    4

    Exclamation Assistance with MsgBox in Program

    OK, it's the newbie to programming. I have written a program to calcuate and display the montly payment and total interest paid on a loan. I have to put in a note that says if interest rate is below .06 that "Interest Rate Must Be 6% or higher." I don't have a problem with the message but after the message is displayed and I hit ok, it still calculates the amount. How do I get it to either, return to program to input new interest rate then complete program: Code is as follows:
    Code:
    Public Class frmLoan
    
        Private Sub btnAnalyze_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnAnalyze.Click
    
            Dim p, r, n, payment, ti As Double
            ' Set MSGbox if percentage is off
            If r < 0.06 Then
                MsgBox("Interest rate must be 6 percent or higher", MsgBoxStyle.Exclamation, "WARNING")
            Else
    
            End If
    
            'set value parameters for calculation
            p = CDbl(txtloan.Text)
            r = CDbl(txtRate.Text) / 12
            n = CDbl(txtLength.Text) ' duration of loan
            payment = (p * r) / (1 - (1 + r) ^ (-n))
            ti = (n * payment) - p
    
    
            'display results from formula calculations
            With lstResults.Items
                .Clear()
                .Add("Total monthly pay is " & (FormatCurrency(payment) & "."))
                .Add("")
                .Add("Total interest is paid is " & (FormatCurrency(ti) & "."))
    
            End With
    
        End Sub
    End Class
    Last edited by Hack; Jul 17th, 2007 at 06:50 AM. Reason: Added [code][/code] tags

  2. #2
    Interweb adm/o/distrator Paul M's Avatar
    Join Date
    Nov 2006
    Location
    Australia, Melbourne
    Posts
    2,306

    Re: Assistance with MsgBox in Program

    All the other code (calculations) should go under the else in the if statement. So if the amount is > 0.06 then it will calculate.

  3. #3
    I don't do your homework! opus's Avatar
    Join Date
    Jun 2000
    Location
    Good Old Europe
    Posts
    3,863

    Re: Assistance with MsgBox in Program

    Welcome to the forum!
    If you would use the code-tags, we could read your code more easily.
    You checked for r<.06 and if that's true you show the messagebox, I figure you don't want to calculate with a interest <.06, if that's true put EXIT SUB after the messagebox-line. That way the calculation will not be done. I would also suggest to put the focus on the control where the interest is set (that should be text1.text) and maybe put in a correct value as well.
    You're welcome to rate this post!
    If your problem is solved, please use the Mark thread as resolved button


    Wait, I'm too old to hurry!

  4. #4
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333

    Re: Assistance with MsgBox in Program

    If there is no Else condition, then you don't need it.
    Code:
         If r < 0.06 Then
                MsgBox("Interest rate must be 6 percent or higher", MsgBoxStyle.Exclamation, "WARNING") 
            End If
    However, in this case, I believe there will be an Else condition as stated by Hell-Lord. That would be where your alternate calculation would go.

  5. #5
    I don't do your homework! opus's Avatar
    Join Date
    Jun 2000
    Location
    Good Old Europe
    Posts
    3,863

    Re: Assistance with MsgBox in Program

    It should be either:
    Code:
    If r < 0.06 Then
                MsgBox("Interest rate must be 6 percent or higher", MsgBoxStyle.Exclamation, "WARNING") 
                Exit Sub
            End If
    ....
    ....
    or
    Code:
    If r < 0.06 Then
                MsgBox("Interest rate must be 6 percent or higher", MsgBoxStyle.Exclamation, "WARNING") 
    Else
      ......
      ......          
    End If
    Where " .... ...." denotes all the rest of the calculations!
    You're welcome to rate this post!
    If your problem is solved, please use the Mark thread as resolved button


    Wait, I'm too old to hurry!

  6. #6
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Assistance with MsgBox in Program

    Quote Originally Posted by opus
    It should be either:
    Code:
    If r < 0.06 Then
                MsgBox("Interest rate must be 6 percent or higher", MsgBoxStyle.Exclamation, "WARNING") 
                Exit Sub
            End If
    ....
    ....
    or
    Code:
    If r < 0.06 Then
                MsgBox("Interest rate must be 6 percent or higher", MsgBoxStyle.Exclamation, "WARNING") 
    Else
      ......
      ......          
    End If
    Where " .... ...." denotes all the rest of the calculations!
    The second option should always be preferred.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  7. #7
    I don't do your homework! opus's Avatar
    Join Date
    Jun 2000
    Location
    Good Old Europe
    Posts
    3,863

    Re: Assistance with MsgBox in Program

    If you say so.

    BTW Knut doesn't look that cute anymore!
    You're welcome to rate this post!
    If your problem is solved, please use the Mark thread as resolved button


    Wait, I'm too old to hurry!

  8. #8
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Assistance with MsgBox in Program

    Using an Exit Sub is creating spaghetti code, although it's not an extreme example thereof. The second option is more efficient and easier to read.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  9. #9
    I don't do your homework! opus's Avatar
    Join Date
    Jun 2000
    Location
    Good Old Europe
    Posts
    3,863

    Re: Assistance with MsgBox in Program

    OK, I take it, Knut!
    You're welcome to rate this post!
    If your problem is solved, please use the Mark thread as resolved button


    Wait, I'm too old to hurry!

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