469,292 Members | 1,310 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,292 developers. It's quick & easy.

Integers/Strings

When asking a user to input a number it's easy to make sure it's higher,
lower etc then a specified amount. What I'm struggling with is if you ask
the user for a number and they enter a letter. How do you code this kind of
thing to make sure that a string is entered as a string or an integer is
entered as an integer?
I'm new to VB so I apologise for asking a question that is probably very
easy.

Jul 17 '05 #1
9 3599
Sorry for the double post, it seemed the first post hadn't been sent so a
phone call to blueyonder sorted it out.
Jul 17 '05 #2
On Thu, 16 Oct 2003 11:49:35 +0100, "Roy Riddex"
<ro**************@blueyonder.co.uk> wrote:
When asking a user to input a number it's easy to make sure it's higher,
lower etc then a specified amount. What I'm struggling with is if you ask
the user for a number and they enter a letter. How do you code this kind of
thing to make sure that a string is entered as a string or an integer is
entered as an integer?
I'm new to VB so I apologise for asking a question that is probably very
easy.


Look at Val() and Str|()

If Val( Text1.Text ) > 123 Then Call Barf

Personally I like creating things back into a String and testing them
again - it is ... belt and braces
Jul 17 '05 #3
"J French" <er*****@nowhere.com> wrote in message
news:3f***************@news.btclick.com...
On Thu, 16 Oct 2003 11:49:35 +0100, "Roy Riddex"
<ro**************@blueyonder.co.uk> wrote:
When asking a user to input a number it's easy to make sure it's higher,
lower etc then a specified amount. What I'm struggling with is if you ask
the user for a number and they enter a letter. How do you code this kind ofthing to make sure that a string is entered as a string or an integer is
entered as an integer?
I'm new to VB so I apologise for asking a question that is probably very
easy.


Look at Val() and Str|()

If Val( Text1.Text ) > 123 Then Call Barf

Personally I like creating things back into a String and testing them
again - it is ... belt and braces

Are you saying that Val(Text1.Text) will always be greater than an Integer
no matter what the letters entered are?
What is 'Call Barf'?
Jul 17 '05 #4
Look at Val() and Str|()

If Val( Text1.Text ) > 123 Then Call Barf

Personally I like creating things back into a String and testing them
again - it is ... belt and braces

Are you saying that Val(Text1.Text) will always be greater than an Integer
no matter what the letters entered are?
What is 'Call Barf'?

I tried your method in a simple form using an Input box and an Ok Command
button. However, it still didn't work. Below is the code, if possible can
someone please check it over and tell me where I'm going wrong. All I want
to do is to make sure that a number is entered and not a letter.

Private Sub cmdOK_Click()
Dim Number As Integer
If Val(Text1.Text) > 123 Then
Text1.Text = ""
Text1.SetFocus
Else
Number = Text1.Text
Print Number
End If
End Sub
Jul 17 '05 #5
In the KeyDown event of your textbox you could test

if (KeyCode < 48 or Keycode > 57) And Keycode <> 45 And Keycode <> 46 Then
KeyCode = 0
End if

This will simply dissallow the users from entering letters in the first
place which will save you validation :-)....48 to 57 is 0 through 9
respectively...45 is the - sign and 46 is the . (dot if you wnat to allow
decimal values).

--
Stéphane Richard
"Ada World" Webmaster
http://www.adaworld.com
"Roy Riddex" <ro**************@blueyonder.co.uk> wrote in message
news:an****************@news-binary.blueyonder.co.uk...
Look at Val() and Str|()

If Val( Text1.Text ) > 123 Then Call Barf

Personally I like creating things back into a String and testing them
again - it is ... belt and braces

Are you saying that Val(Text1.Text) will always be greater than an Integer no matter what the letters entered are?
What is 'Call Barf'?

I tried your method in a simple form using an Input box and an Ok Command
button. However, it still didn't work. Below is the code, if possible can
someone please check it over and tell me where I'm going wrong. All I want
to do is to make sure that a number is entered and not a letter.

Private Sub cmdOK_Click()
Dim Number As Integer
If Val(Text1.Text) > 123 Then
Text1.Text = ""
Text1.SetFocus
Else
Number = Text1.Text
Print Number
End If
End Sub

Jul 17 '05 #6
> In the KeyDown event of your textbox you could test

if (KeyCode < 48 or Keycode > 57) And Keycode <> 45 And Keycode <> 46 Then KeyCode = 0
End if

This will simply dissallow the users from entering letters in the first
place which will save you validation :-)....48 to 57 is 0 through 9
respectively...45 is the - sign and 46 is the . (dot if you wnat to allow
decimal values).


But it won't protect against the user pasting in bad data.

Rick - MVP
Jul 17 '05 #7
> ....<snip>... All I want to do is to make sure that a number
is entered and not a letter.


To decide if the user's entry is a number or not, here are two functions
that I have posted in the past for similar questions..... one is for digits
only and the other is for "regular" numbers.

Function IsDigitsOnly(Value As String) As Boolean
IsDigitsOnly = Not Value Like "*[!0-9]*"
End Function

Function IsNumber(ByVal Value As String) As Boolean
' Leave the next statement out if you don't
' want to provide for plus/minus signs
If Value Like "[+-]*" Then Value = Mid$(Value, 2)
IsNumber = Not Value Like "*[!0-9.]*" And _
Not Value Like "*.*.*" And _
Len(Value) > 0 And Value <> "." And _
Value <> vbNullString
End Function

Rick - MVP
Jul 17 '05 #8
Just use the IsNumeric() function to check the value before doing anything
with it. Try the code sample below with "123" and "123b" and see if that is
what you are looking for.

Dim strMyString As String
strMyString = "123b"
'strMyString = "123"

If IsNumeric(strMyString) Then
MsgBox strMyString & " is numeric."
Else
MsgBox strMyString & " is not numeric."
End If

HTH,
K
"Roy Riddex" <ro**************@blueyonder.co.uk> wrote in message
news:CI***************@news-binary.blueyonder.co.uk...
When asking a user to input a number it's easy to make sure it's higher,
lower etc then a specified amount. What I'm struggling with is if you ask
the user for a number and they enter a letter. How do you code this kind of thing to make sure that a string is entered as a string or an integer is
entered as an integer?
I'm new to VB so I apologise for asking a question that is probably very
easy.

Jul 17 '05 #9
> Just use the IsNumeric() function to check the value before doing anything
with it. Try the code sample below with "123" and "123b" and see if that is what you are looking for.

Dim strMyString As String
strMyString = "123b"
'strMyString = "123"

If IsNumeric(strMyString) Then
MsgBox strMyString & " is numeric."
Else
MsgBox strMyString & " is not numeric."
End If

HTH,
K


I usually try and steer people away from using IsNumeric to "proof"
supposedly numeric text. Consider this (also see note at end of post):

ReturnValue = IsNumeric("($1,23,,3.4,,,5,,E67$)")

Most people would not expect THAT to return True. IsNumeric has some "flaws"
in what it considers a proper number and what most programmers are looking
for.

I had a short tip published by Pinnacle Publishing in their Visual Basic
Developer magazine that covered some of these flaws. Originally, the tip was
free to view but is now viewable only by subscribers.. Basically, it said
that IsNumeric returned True for things like -- currency symbols being
located in front or in back of the number as shown in my example (also
applies to plus, minus and blanks too); numbers surrounded by parentheses as
shown in my example (some people use these to mark negative numbers);
numbers containing any number of commas before a decimal point as shown in
my example; numbers in scientific notation (a number followed by an upper or
lower case "D" or "E", followed by a number equal to or less than 305 -- the
maximum power of 10 in VB); and Octal/Hexadecimal numbers (&H for
Hexadecimal, &O or just & in front of the number for Octal).

NOTE:
======
In the above example and in the referenced tip, I refer to $ signs and
commas and dots -- these were meant to refer to your currency, thousands
separator and decimal point symbols as defined in your local settings --
substitute your local regional symbols for these if appropriate.

As for your question about checking numbers, here are two functions that I
have posted in the past for similar questions..... one is for digits only
and the other is for "regular" numbers:

Function IsDigitsOnly(Value As String) As Boolean
IsDigitsOnly = Not Value Like "*[!0-9]*"
End Function

Function IsNumber(ByVal Value As String) As Boolean
' Leave the next statement out if you don't
' want to provide for plus/minus signs
If Value Like "[+-]*" Then Value = Mid$(Value, 2)
IsNumber = Not Value Like "*[!0-9.]*" And _
Not Value Like "*.*.*" And _
Len(Value) > 0 And Value <> "." And _
Value <> vbNullString
End Function

Rick - MVP
Jul 17 '05 #10

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

5 posts views Thread by Jørgen Cederberg | last post: by
5 posts views Thread by beliavsky | last post: by
16 posts views Thread by aruna | last post: by
23 posts views Thread by Summercool | last post: by
4 posts views Thread by Shisou | last post: by
reply views Thread by suresh191 | last post: by
reply views Thread by harlem98 | last post: by
1 post views Thread by Geralt96 | last post: by
reply views Thread by harlem98 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.