|
-
May 8th, 2002, 09:11 PM
#1
Thread Starter
New Member
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
-
May 8th, 2002, 09:16 PM
#2
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:
Private Sub txtYExperience_KeyPress(KeyAscii As Integer)
If Not Chr$(KeyAscii) Like "[0-9]" And KeyAscii <> vbKeyBack Then
KeyAscii = 0
End If
End Sub
Laugh, and the world laughs with you. Cry, and you just water down your vodka.
Take credit, not responsibility
-
May 8th, 2002, 09:21 PM
#3
PowerPoster
Or you can use this:
VB Code:
If IsNull(Text1.Text) Then
lblValidYexperience = "No"
Else
'if all are numbers
If IsNumeric(Text1.Text) Then
If (Int(Text1.Text) >= 0) And (Int(Text1.Text) <= 50) Then
lblValidYexperience = "Yes"
Else
lblValidYexperience = "No"
End If
Else
lblValidYexperience = "No"
End If
End If
-
May 8th, 2002, 09:24 PM
#4
[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
-
May 8th, 2002, 09:26 PM
#5
Thread Starter
New Member
thank you
wow that was fast
thank you speedy gonzales
I'l give that a try.
-
May 8th, 2002, 09:26 PM
#6
Originally posted by crptcblade
If Not Chr$(KeyAscii) Like "[0-9]" And...
Man this "Like" code is damn good!
-
May 8th, 2002, 10:37 PM
#7
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:
Private Sub Text1_LostFocus()
If IsNumeric(Text1.Text) And _
Val(Text1.Text) >= 0 And _
Val(Text1.Text) <= 50 Then
Label1.Caption = "OK"
Else
Label1.Caption = "Not 0-50"
End If
End Sub
-
May 9th, 2002, 12:43 AM
#8
Addicted Member
This exemple allouws numeric key's and backspace
put it in to a module
call it -> ContrNum KeyAscii
VB Code:
' Numeric input control
Public Sub ContrNum(Key As Integer)
If (Key< 48 Or Key> 57) And Key<> 8 Then
MsgBox "Your message "
Key= 0
End If
End Sub
-
May 9th, 2002, 01:56 AM
#9
Thread Starter
New Member
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|