PC Review


Reply
Thread Tools Rate Thread

checking for a numeric value in a textbox

 
 
=?Utf-8?B?bWFyaw==?=
Guest
Posts: n/a
 
      29th Dec 2006
Hi.

I have a couple of textboxes on a vb form. For these, I need the value
entered to be sure to be numeric.

What's the best way to check for that?

application.isnumber(txtBox.value) returns false... becauese it thinks its
text.

the Val(txtBox.value) function does some checking, but it will tell you that
Val(5a) = 5, etc.

I think one time I did some checking on the len of Val(), as compared to the
original len of the entry, but that probably has holes too.

Please point me toward the simple way.

Thanks.

 
Reply With Quote
 
 
 
 
Mike Fogleman
Guest
Posts: n/a
 
      29th Dec 2006
If IsNumeric(TextBox1.Value) = True Then
' do nothing
Else
TextBox1.Value = ""
End If

Something like that..If the textbox is on a UserForm you may have to qualify
it more
Me.TextBox1 or UserForm1.TextBox1 etc.
Mike F
"mark" <(E-Mail Removed)> wrote in message
news:58E3BA55-E389-4387-A4BB-(E-Mail Removed)...
> Hi.
>
> I have a couple of textboxes on a vb form. For these, I need the value
> entered to be sure to be numeric.
>
> What's the best way to check for that?
>
> application.isnumber(txtBox.value) returns false... becauese it thinks its
> text.
>
> the Val(txtBox.value) function does some checking, but it will tell you
> that
> Val(5a) = 5, etc.
>
> I think one time I did some checking on the len of Val(), as compared to
> the
> original len of the entry, but that probably has holes too.
>
> Please point me toward the simple way.
>
> Thanks.
>



 
Reply With Quote
 
=?Utf-8?B?TWFydGluIEZpc2hsb2Nr?=
Guest
Posts: n/a
 
      30th Dec 2006
Mark,

The following works for me:

If Not IsNumeric(Me.TextBox1.Value) Then
MsgBox "not a number"
End If

The other way to do it is to limit the charaters on the text box as in:

Private Sub TextBox1_Change()

Dim ptr As Long
Dim sNew As String
Dim sCur As String
Dim bPoint As Boolean
Dim charOK As Boolean

For ptr = 1 To Len(Me.TextBox1.Value)
charOK = False
sCur = Mid(Me.TextBox1.Value, ptr, 1)
Select Case sCur
Case "0" To "9"
charOK = True
Case "+", "-"
If ptr = 1 Then charOK = True
Case "."
If Not bPoint Then
charOK = True
bPoint = True
End If
End Select

If charOK Then
sNew = sNew & sCur
Else
Beep
End If
Next ptr
Me.TextBox1.Value = sNew
End Sub

This limits the characters that are accepted on input.

--
Hope this helps
Martin Fishlock, Bangkok, Thailand
Please do not forget to rate this reply.


"mark" wrote:

> Hi.
>
> I have a couple of textboxes on a vb form. For these, I need the value
> entered to be sure to be numeric.
>
> What's the best way to check for that?
>
> application.isnumber(txtBox.value) returns false... becauese it thinks its
> text.
>
> the Val(txtBox.value) function does some checking, but it will tell you that
> Val(5a) = 5, etc.
>
> I think one time I did some checking on the len of Val(), as compared to the
> original len of the entry, but that probably has holes too.
>
> Please point me toward the simple way.
>
> Thanks.
>

 
Reply With Quote
 
Chip Pearson
Guest
Posts: n/a
 
      30th Dec 2006
Mark,

See http://www.cpearson.com/excel/TextBox.htm


--
Cordially,
Chip Pearson
Microsoft MVP - Excel
Pearson Software Consulting, LLC
www.cpearson.com
(email address is on the web site)


"mark" <(E-Mail Removed)> wrote in message
news:58E3BA55-E389-4387-A4BB-(E-Mail Removed)...
> Hi.
>
> I have a couple of textboxes on a vb form. For these, I need the value
> entered to be sure to be numeric.
>
> What's the best way to check for that?
>
> application.isnumber(txtBox.value) returns false... becauese it thinks its
> text.
>
> the Val(txtBox.value) function does some checking, but it will tell you
> that
> Val(5a) = 5, etc.
>
> I think one time I did some checking on the len of Val(), as compared to
> the
> original len of the entry, but that probably has holes too.
>
> Please point me toward the simple way.
>
> Thanks.
>



 
Reply With Quote
 
Francisco Parrilla
Guest
Posts: n/a
 
      30th Dec 2006
Del foro correspondiente:
Esta macro cotniene unas funciones que se pueden llamar desde cualquier
TextBox
y que limitan los datos que se pueden escribir en el.
Consta de las siguientes Funciones
OnlyNumbers : permite escribir solo numeros en el TextBox
OnlyChar: permite escribir solo letras
MaxChar: limita el numero de caracteres que se pueden escribir en un TextBox
y opcinalmente da un mensaje.

http://www.exceluciones.com/portal/download.php?id=7414

link original:
http://www.exceluciones.com/portal/v...?p=33958#33958

--
Atte.
?T Francisco T?
http://groups.msn.com/ExcelVbaMacrosOffice

El verdadero buscador crece y aprende, y descubre que siempre es el
principal responsable de lo que sucede.
Jorge Bucay

La oscuridad nos envuelve a todos, pero mientras el sabio tropieza en alguna
pared, el ignorante permanece tranquilo en el centro de la estancia.
Anatole France


 
Reply With Quote
 
=?Utf-8?B?bWFyaw==?=
Guest
Posts: n/a
 
      30th Dec 2006
Thanks everybody.

Don't know why I wasn't seeing the IsNumeric earlier...

And Chip's pages are always good.

Thanks. Got it now.
 
Reply With Quote
 
=?Utf-8?B?bWFyaw==?=
Guest
Posts: n/a
 
      30th Dec 2006
Hola, Francisco,

Gracias.

I can read what you wrote, mostly, but am not good enough in Spanish to
write back well.

Gracias.
Mark

"Francisco Parrilla" wrote:

> Del foro correspondiente:
> Esta macro cotniene unas funciones que se pueden llamar desde cualquier
> TextBox
> y que limitan los datos que se pueden escribir en el.
> Consta de las siguientes Funciones
> OnlyNumbers : permite escribir solo numeros en el TextBox
> OnlyChar: permite escribir solo letras
> MaxChar: limita el numero de caracteres que se pueden escribir en un TextBox
> y opcinalmente da un mensaje.
>
> http://www.exceluciones.com/portal/download.php?id=7414
>
> link original:
> http://www.exceluciones.com/portal/v...?p=33958#33958
>
> --
> Atte.
> ?T Francisco T?
> http://groups.msn.com/ExcelVbaMacrosOffice
>
> El verdadero buscador crece y aprende, y descubre que siempre es el
> principal responsable de lo que sucede.
> Jorge Bucay
>
> La oscuridad nos envuelve a todos, pero mientras el sabio tropieza en alguna
> pared, el ignorante permanece tranquilo en el centro de la estancia.
> Anatole France
>
>
>

 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Checking if a string is numeric... moo Microsoft C# .NET 9 27th Jul 2007 01:52 AM
Checking For Numeric Before Computing? PeteCresswell Microsoft Excel Discussion 4 11th Jul 2007 07:35 PM
Re: Checking numeric Maqsood Ahmed Microsoft Dot NET Framework Forms 0 15th May 2005 03:10 PM
Checking for non-numeric input =?Utf-8?B?RGF2aWQgQ2xlYXZl?= Microsoft Access Forms 2 18th Jan 2005 01:51 PM
Checking for numeric value Simon Harris Microsoft Access 7 4th Nov 2003 06:49 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 03:52 AM.