View Single Post
  #3   Report Post  
Posted to microsoft.public.excel.programming
Martin Fishlock Martin Fishlock is offline
external usenet poster
 
Posts: 694
Default checking for a numeric value in a textbox

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.