Results 1 to 9 of 9

Thread: Better place for beginners?

Hybrid View

  1. #1

    Thread Starter
    New Member
    Join Date
    May 2002
    Posts
    5

    Better place for beginners?

    Is there another forum that would be better to ask beginner questions? If so let me know, I'll ask there.

    My problem is simple, I'm trying to let a user input a number range 0-50 into a text box, then return valid or not valid in a label.

    My problem using val() returns valid whenever the user inputs a letters or a blank (I have to keep 0 as an option) Is there a way to force val to return null or something similiar so that my range doesnt pick it up?

    Alternatively I've tried using IsNumeric without much success. Its entirely probable that I'm just doing something wrong.

    Here is my code after I gave up on val()


    Private Sub txtYexperience_LostFocus()

    Dim intYexperience As Integer
    intYexperience = txtYeperience

    If IsNumeric(intYexperience) = False Then
    lblValidYexperience = "No"

    Else
    If (intYexperience >= 0) And (intYexperience <= 50) = True Then
    lblValidYexperience = "Yes"
    Else lblValidYexperience = "No"
    End If

    End If

    End Sub

    Thank you,
    OregonStudent

  2. #2
    The Devil crptcblade's Avatar
    Join Date
    Aug 2000
    Location
    Quetzalshacatenango
    Posts
    9,091
    You can use CDbl() which will return an error if there is non-numeric characters. Or you can limit the input to numbers only
    VB Code:
    1. Private Sub txtYExperience_KeyPress(KeyAscii As Integer)
    2.  
    3.     If Not Chr$(KeyAscii) Like "[0-9]" And KeyAscii <> vbKeyBack Then
    4.         KeyAscii = 0
    5.     End If
    6.  
    7. End Sub

    Laugh, and the world laughs with you. Cry, and you just water down your vodka.


    Take credit, not responsibility

  3. #3
    PowerPoster abdul's Avatar
    Join Date
    Dec 2000
    Location
    Ontario,Canada
    Posts
    2,827
    Or you can use this:
    VB Code:
    1. If IsNull(Text1.Text) Then
    2. lblValidYexperience = "No"
    3. Else
    4. 'if all are numbers
    5. If IsNumeric(Text1.Text) Then
    6. If (Int(Text1.Text) >= 0) And (Int(Text1.Text) <= 50) Then
    7. lblValidYexperience = "Yes"
    8. Else
    9. lblValidYexperience = "No"
    10. End If
    11. Else
    12. lblValidYexperience = "No"
    13. End If
    14. End If
    Baaaaaaaaah

  4. #4
    Stiletto
    Guest
    [QOUTE]
    Is there another forum that would be better to ask beginner questions? If so let me know, I'll ask there. [/QOUTE]
    I think this is the right place

  5. #5

    Thread Starter
    New Member
    Join Date
    May 2002
    Posts
    5

    thank you

    wow that was fast

    thank you speedy gonzales

    I'l give that a try.

  6. #6
    Stiletto
    Guest
    Originally posted by crptcblade

    If Not Chr$(KeyAscii) Like "[0-9]" And...
    Man this "Like" code is damn good!

  7. #7
    WorkHorse
    Guest
    I don't see anyhing wrong with the original code to validate the text box other than not using Text and Caption properties.

    VB Code:
    1. Private Sub Text1_LostFocus()
    2.     If IsNumeric(Text1.Text) And _
    3.        Val(Text1.Text) >= 0 And _
    4.        Val(Text1.Text) <= 50 Then
    5.         Label1.Caption = "OK"
    6.     Else
    7.         Label1.Caption = "Not 0-50"
    8.     End If
    9. End Sub

  8. #8
    Addicted Member
    Join Date
    Feb 2002
    Location
    Belgium
    Posts
    190
    This exemple allouws numeric key's and backspace

    put it in to a module
    call it -> ContrNum KeyAscii

    VB Code:
    1. ' Numeric input control
    2. Public Sub ContrNum(Key As Integer)
    3.    If (Key< 48 Or Key> 57) And Key<> 8 Then
    4.       MsgBox "Your message                          "
    5.       Key= 0
    6.    End If
    7. End Sub

  9. #9

    Thread Starter
    New Member
    Join Date
    May 2002
    Posts
    5

    Thanks guys

    Thanks again guys, great responses. I ended up using Workhorses code (which was similiar to my original) and then throwing on crptcblade's for fun, adding on an annoying Beep for each keystroke that isnt a a number. That'll teach'em.

    This is my first time visiting the forums section of VB-world. I just looked at the clock and realized I spent 2 hours going over the lasts few pages of messages. I know so little, but just doing that I learned quite a few interesting things. For example, I noticed Workhorse using _ to make the layout nice and tidy.(that tells you how little I know) Why the professor didn't mention that I have no idea. There are so many little things like that, that you folks who are VB wizards probably never even think about. Great stuff for me though.

    Thanks again,
    OS

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