vb data validation
Results 1 to 8 of 8

Thread: vb data validation

  1. #1

    vb data validation

    I'm trying to make sure that the data given by 2 text fields is no more than 60, but when i enter 10 in the first (txthoursworked) and 5 in the second (txtovertime) it returns true. The code below is what i`m using.

    'check numeric data
    If (txthoursworked.Text + txtovertime.Text > 60) Then
    MsgBox "An employee cannot work over 60 hours"
    End
    End If

    I`ve tried storing txthoursworked.Text + txtovertime.Text in a variable and using that but its the same, i tried moving around the brackets but that did nothing either, what's wrong? How can 5+10 equal > 60 ?

  2. #2
    I'd rather be fishing DjM's Avatar
    Join Date
    Aug 2001
    Location
    The Great White North
    Posts
    1,867
    Maybe I am reading this wrong, but, 10 + 5 = 15, 15 is less than 60 therefore your code is working is it not?

    Try puting in 50 in one field and 20 in the other and see what happens.


    Cheers:
    DjM

  3. #3
    Senior Member
    Join Date
    Mar 2003
    Location
    central il
    Posts
    1,779
    I gforgot the conversion...but are the feilds storeing as a string rather then a integer if so you need to convert to integers for the math there is a simple function to do this.

    DJM you reversed the sign 15!>60 so the code isnt working
    Who is more trustworthy then all of the gurus or Buddha’s?

  4. #4
    uhm, they're not being stored as anything because i`m reading them from field box state, not reading it from a variable.

    And if my code was working this post wouldn`t exist.

  5. #5
    Senior Member
    Join Date
    Mar 2003
    Location
    central il
    Posts
    1,779
    right...but If i rember correctly the text box is a string...you are adding the asci reprsentation of 5 to the asci represintation of 15 (or perhaps concatinating..don't remember how vb handles a + in strings/charicters) and then checking to see if its > the asci representation of 60. what you want to do I think is

    If (CLng(txthoursworked.Text) +CLng (txtovertime.Text) > 60) Then

    check the syntax on that though as I am a VB hack. More of a Perl/C++ guy.
    Who is more trustworthy then all of the gurus or Buddha’s?

  6. #6
    thanks alot, it seems to work.

    only problem is it gets an error if the user enters a string and not a letter, do you know how to test for prescense of a string? Or how to test with an IF statement for failure, i.e if(isnot(string), microsoft nor google can tell me a way to test for prescense of a string or how to test for if something is not something.

  7. #7
    Senior Member
    Join Date
    Jan 2003
    Posts
    3,914
    Use IsNumeric...

    put your if statement inside another if statement

    If IsNumeric(txtHoursWorked.text) and IsNumeric(txtOvertime.text) Then
    <Previous If Statement>
    Else
    Msgbox("Please Enter a Numeric Value")
    End If

  8. #8
    Thank you very much for your help.

    I hate VB and have to pretend to know it for college, but it's the only programming language the teachers know so we get lumbered with it too.

    Once again thank you for everyone who's helped me out.

Posting Permissions

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