Vb5 Validation code inside

Associate
Joined
22 May 2004
Posts
1,792
Location
N.Ireland
Hi guys am doing a program for tech and have been told to add validation.Ie a message box error if user inputs the incorrect value into the text boxes. The user should be entering Int or doubles. So if a user adds text how can I get the program to throw up an error. The code in question is below.

Code:
 Private Sub btnCalculate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCalculate.Click
        'Declare Local Variables required for calculations
        Dim dblInterest, dblInvested, dblYears As Double
        Dim dblInterestEarned, dblTotal As Double

        'Input Values from User
        dblInterest = Val(Me.txtInterest.Text)
        dblInvested = Val(Me.txtAmount.Text)
        dblYears = Val(Me.txtYears.Text)


        'Perform Calculations based upon input Values
        dblInterestEarned = dblInvested * (1 + dblInterest / 100) ^ dblYears
        dblTotal = dblInterestEarned + dblInvested

        'Output results to Form
        Me.txtInterestEarned.Text = CStr(dblInterestEarned)
        Me.txtAmountEarned.Text = CStr(dblTotal)

Any help would be appricated
 
Its been a very long time since I did anything VB based.

Just had a browse through one of my vb6 books as I can't find the vb5 one.

Look at the KeyPress event.
Code:
If keyAscii >= Asc("0") And KeyAscii <= Asc ("9") Then KeyAscii = 0

That code will not send an error message and stops the user entering any number into a text box. However, I am sure that it can point you in the right direction.

I am sure someone will be able to tell you if i am talking testicles :D
 
Hi guys one more easy one I know Me.txtAmount.Text = Format(dblInvested, "Currency") formats for Currency how do I get anyother one to format for a percentage ie 10%
 
Ok Ive got a bit further with the error thing.
Code:
Public Class frmCompound

    Private Sub btnCalculate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCalculate.Click
        'Declare Local Variables required for calculations
        Dim dblInterest, dblInvested, dblYears As Double
        Dim dblInterestEarned, dblTotal As Double
        ' Dim , As Double



        'Input Values from User
        dblInterest = Val(Me.txtInterest.Text)
        'Me.txtInterest.Text = Format(dblInterest, "percentage")
        dblInvested = Val(Me.txtAmount.Text)
        Me.txtAmount.Text = Format(dblInvested, "Currency")
        dblYears = Val(Me.txtYears.Text)

        'Test for Invaild Input
        If IsNumeric(Me.txtInterest.Text) Then
        Else
            'Output a suitable error message
            MessageBox.Show("You must enter a number!", "Invalid Input", MessageBoxButtons.OK, MessageBoxIcon.Error)
            If IsNumeric(Me.txtAmount.Text) Then

            End If
        End If






        'Perform Calculations based upon input Values
        dblInterestEarned = dblInvested * (1 + dblInterest / 100) ^ dblYears
        dblInterestEarned = dblInterestEarned - dblInvested
        dblTotal = dblInvested + dblInterestEarned

        'Output results to Form
        Me.txtInterestEarned.Text = CStr(dblInterestEarned)
        Me.txtInterestEarned.Text = Format(dblInterestEarned, "Currency")
        Me.txtAmountEarned.Text = CStr(dblTotal)
        Me.txtAmountEarned.Text = Format(dblTotal, "Currency")

    End Sub

    Private Sub btnClear_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnClear.Click
        Me.txtAmount.Text = ""
        Me.txtAmountEarned.Text = ""
        Me.txtInterest.Text = ""
        Me.txtInterestEarned.Text = ""
        Me.txtYears.Text = ""
    End Sub

    Private Sub BtnExitMenu_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnExitMenu.Click
        Me.Hide()
        frmMenu.Show()
    End Sub
End Class

And got it showing error if you dont put a value in the interest box but I cant get it to work with the amount and years box as well Please help
 
Back
Top Bottom