RE: How2 accept only numbers in userform textbox



Marty,
Write code to Validate the user input BEFORE you perform you math functions.
If the data validates, then do the math. If not, you can use the msgbox
function to prompt the user that some or all of the data is invalid.

You can validate each text box, one at a time, using the 'TextBox1_Exit'
procedure or validate them all at the same time. I prefer to validate them
all at the same time and then tell the user where the mistakes are. The
following code is an example tht you can build from.

Private Sub CommandButton1_Click()
If mAcceptChar(TextBox1.Text, "0123456789") = False Then
MsgBox "Booboo", vbOKOnly
End If
End Sub
Private Function mAcceptChar(sText As String, sCharSet As String) As Boolean
Dim sChar As String
Dim X As Integer

For X = 1 To Len(sText)
sChar = Mid$(sText, X, 1)
If InStr(1, sCharSet, sChar) > 0 Then
mAcceptChar = True
Else
mAcceptChar = False
Exit For
End If
Next X
End Function

The easiest way is the simply determine if the text is numeric as in

Private Sub CommandButton1_Click()
If Not IsNumeric(TextBox1.Text) Then
MsgBox "Booboo", vbOKOnly
End If
End Sub

"MARTY" wrote:

Hello:

I have a useform which requires a user to input data into some textboxes.
The user then clicks a button and some math is done on the data input by the
user.

The problem is that right now there is nothing to prevent the user from
putting anything (text, symbols or whatever) into the boxes, and this causes
a runtime error when the button is clicked.

I'm looking for some code to put into my userform which will generate an
error message if the user inputs anything other than a number into one of the
textboxes.

Help would be appreciated.

Thanks,
MARTY
.