Test for Whole Number

  • Follow


I am probably making this harder than it needs to be.  What is a good way to 
test if a user enters a whole number as a data input.  I cobbled together 
the following but is there a simplier way?

Sub Test()
Dim pInt  As Integer
Dim pStr As String
Do
  pStr = InputBox("Enter a whole number")
  pInt = pStr
Loop While pInt <> pStr
MsgBox "Thank you"
End Sub

-- 
Greg Maxey

See my web site http://gregmaxey.mvps.org
for an eclectic collection of Word Tips.

Arrogance is a weed that grows mostly on a dunghill (Arabic proverb)



0
Reply Greg 11/14/2009 1:39:35 PM

I am sure there are other ways that may be better, but how about

Sub Test()
Dim pInt  As Integer
Start:
On Error Resume Next
pInt = Int(InputBox("Enter a whole number"))
If Err.Number = 13 Then
    MsgBox "Enter a number!"
    GoTo Start
End If
MsgBox pInt & " Thank you"
End Sub

-- 
<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
Graham Mayor -  Word MVP

My web site www.gmayor.com
Word MVP web site http://word.mvps.org
<>>< ><<> ><<> <>>< ><<> <>>< <>><<>


Greg Maxey wrote:
> I am probably making this harder than it needs to be.  What is a good
> way to test if a user enters a whole number as a data input.  I
> cobbled together the following but is there a simplier way?
>
> Sub Test()
> Dim pInt  As Integer
> Dim pStr As String
> Do
>  pStr = InputBox("Enter a whole number")
>  pInt = pStr
> Loop While pInt <> pStr
> MsgBox "Thank you"
> End Sub 


0
Reply Graham 11/14/2009 2:02:33 PM


On Sat, 14 Nov 2009 08:39:35 -0500, "Greg Maxey"
<gmaxey@mIKEvICTORpAPAsIERRA.oSCARrOMEOgOLF> wrote:

>I am probably making this harder than it needs to be.  What is a good way to 
>test if a user enters a whole number as a data input.  I cobbled together 
>the following but is there a simplier way?
>
>Sub Test()
>Dim pInt  As Integer
>Dim pStr As String
>Do
>  pStr = InputBox("Enter a whole number")
>  pInt = pStr
>Loop While pInt <> pStr
>MsgBox "Thank you"
>End Sub

Hi Greg,

The trouble with relying on implicit type conversion of a string to an
integer like that is that if the user enters a completely nonnumeric
value such as "A", you get a type mismatch error. To make it reliable,
you need more complication, not less. While I was at it, I added a
check for the Cancel.

Sub Test2()
Dim pInt  As Integer
Dim pStr As String
Do
  pStr = InputBox("Enter a whole number")
  If StrPtr(pStr) = 0 Then Exit Sub
Loop Until IsNumeric(pStr) And (Val(pStr) = CInt(Val(pStr)))
pInt = Val(pStr)
MsgBox "Thank you"
End Sub

--
Regards,
Jay Freedman
Microsoft Word MVP        FAQ: http://word.mvps.org
Email cannot be acknowledged; please post all follow-ups to the newsgroup so all may benefit.
0
Reply Jay 11/14/2009 2:15:35 PM

Graham/Jay,

Thanks.

This is the piece that I was looking for:  (Val(pStr) = CInt(Val(pStr)))

Which I incorporated in what I already had:

If Not (Val(pStr) = CInt(Val(pStr))) Or Not IsNumeric(pStr) _
    Or Not Val(pStr) < 36 Or Not Val(pStr) > 17 Then



Greg Maxey wrote:
> I am probably making this harder than it needs to be.  What is a good
> way to test if a user enters a whole number as a data input.  I
> cobbled together the following but is there a simplier way?
>
> Sub Test()
> Dim pInt  As Integer
> Dim pStr As String
> Do
>  pStr = InputBox("Enter a whole number")
>  pInt = pStr
> Loop While pInt <> pStr
> MsgBox "Thank you"
> End Sub 


0
Reply Greg 11/14/2009 2:38:27 PM

Hi Greg,

If you're working with a UserForm, you could use:
Private Sub TextBox1_KeyPress(ByVal KeyAscii As MSForms.ReturnInteger)
If Not (KeyAscii > 47 And KeyAscii < 59) Then
 Beep
 KeyAscii = 0
End If
End Sub

Attached to TextBox1 on a UserForm, the above code ensures that only numbers can be entered. Any non-numeric keystroke generates a 
beep and the offending character is deleted.

-- 
Cheers
macropod
[Microsoft MVP - Word]


"Greg Maxey" <gmaxey@mIKEvICTORpAPAsIERRA.oSCARrOMEOgOLF> wrote in message news:u47Fl$SZKHA.2164@TK2MSFTNGP02.phx.gbl...
>I am probably making this harder than it needs to be.  What is a good way to test if a user enters a whole number as a data input. 
>I cobbled together the following but is there a simplier way?
>
> Sub Test()
> Dim pInt  As Integer
> Dim pStr As String
> Do
>  pStr = InputBox("Enter a whole number")
>  pInt = pStr
> Loop While pInt <> pStr
> MsgBox "Thank you"
> End Sub
>
> -- 
> Greg Maxey
>
> See my web site http://gregmaxey.mvps.org
> for an eclectic collection of Word Tips.
>
> Arrogance is a weed that grows mostly on a dunghill (Arabic proverb)
>
>
> 

0
Reply macropod 11/15/2009 2:47:53 AM

Thanks.  I think I have used something like that before and it is always 
good to see examples of handy code.

"macropod" <macropod@invalid.invalid> wrote in message 
news:OEkMl3ZZKHA.4932@TK2MSFTNGP02.phx.gbl...
> Hi Greg,
>
> If you're working with a UserForm, you could use:
> Private Sub TextBox1_KeyPress(ByVal KeyAscii As MSForms.ReturnInteger)
> If Not (KeyAscii > 47 And KeyAscii < 59) Then
> Beep
> KeyAscii = 0
> End If
> End Sub
>
> Attached to TextBox1 on a UserForm, the above code ensures that only 
> numbers can be entered. Any non-numeric keystroke generates a beep and the 
> offending character is deleted.
>
> -- 
> Cheers
> macropod
> [Microsoft MVP - Word]
>
>
> "Greg Maxey" <gmaxey@mIKEvICTORpAPAsIERRA.oSCARrOMEOgOLF> wrote in message 
> news:u47Fl$SZKHA.2164@TK2MSFTNGP02.phx.gbl...
>>I am probably making this harder than it needs to be.  What is a good way 
>>to test if a user enters a whole number as a data input. I cobbled 
>>together the following but is there a simplier way?
>>
>> Sub Test()
>> Dim pInt  As Integer
>> Dim pStr As String
>> Do
>>  pStr = InputBox("Enter a whole number")
>>  pInt = pStr
>> Loop While pInt <> pStr
>> MsgBox "Thank you"
>> End Sub
>>
>> -- 
>> Greg Maxey
>>
>> See my web site http://gregmaxey.mvps.org
>> for an eclectic collection of Word Tips.
>>
>> Arrogance is a weed that grows mostly on a dunghill (Arabic proverb)
>>
>>
>>
> 


0
Reply Greg 11/15/2009 3:18:38 AM

Greg Maxey wrote:
>> If you're working with a UserForm, you could use:
>> Private Sub TextBox1_KeyPress(ByVal KeyAscii As MSForms.ReturnInteger)
>> If Not (KeyAscii > 47 And KeyAscii < 59) Then
>> Beep
>> KeyAscii = 0
>> End If
>> End Sub
>
> Thanks.  I think I have used something like that before and it is always
> good to see examples of handy code.

Be aware the user can still paste text into such a textbox...
-- 
..NET: It's About Trust!
 http://vfred.mvps.org 


0
Reply Karl 11/17/2009 12:15:14 AM

Be familiar with handy code and how to get around it is even better.  Thanks 
Karl.

Karl E. Peterson wrote:
> Greg Maxey wrote:
>>> If you're working with a UserForm, you could use:
>>> Private Sub TextBox1_KeyPress(ByVal KeyAscii As
>>> MSForms.ReturnInteger) If Not (KeyAscii > 47 And KeyAscii < 59) Then
>>> Beep
>>> KeyAscii = 0
>>> End If
>>> End Sub
>>
>> Thanks.  I think I have used something like that before and it is
>> always good to see examples of handy code.
>
> Be aware the user can still paste text into such a textbox... 


0
Reply Greg 11/17/2009 12:42:21 AM

Greg Maxey wrote:
> Karl E. Peterson wrote:
>> Greg Maxey wrote:
>>>> If you're working with a UserForm, you could use:
>>>> Private Sub TextBox1_KeyPress(ByVal KeyAscii As
>>>> MSForms.ReturnInteger) If Not (KeyAscii > 47 And KeyAscii < 59) Then
>>>> Beep
>>>> KeyAscii = 0
>>>> End If
>>>> End Sub
>>>
>>> Thanks.  I think I have used something like that before and it is
>>> always good to see examples of handy code.
>>
>> Be aware the user can still paste text into such a textbox...
>
> Be familiar with handy code and how to get around it is even better.  Thanks
> Karl.

What I always find amusing are those developers who don't want you to Copy text from 
their textbox controls, so they thwart the Cntl-C response totally unaware that the 
underlying baseclass has supported Cntl-Insert ever since Windows 1.  Kids, these 
days, huh? <g>
-- 
..NET: It's About Trust!
 http://vfred.mvps.org 


0
Reply Karl 11/17/2009 12:49:24 AM

8 Replies
323 Views

(page loaded in 0.165 seconds)


Reply: