data validatie in userform


Posted by WangYa on December 24, 2001 9:09 PM

How can I make sure that valid data is entered in a textbox in a userform.
Colo gave the suggestion below and that works but still it is possible to enter wrong data such as a row of ,,,,, and numbers like 1,12,345 as well the + and – still work resulting in wrong data. Any good idea’s?

Private Sub TextBox1_Change()
'///Please Paste UserForm Module
If IsNumeric(TextBox1.Value) = False Then
Application.EnableEvents = False
MsgBox "Only numbers please!", vbCritical
TextBox1.Value = Left(TextBox1.Value, Len(TextBox1.Value) - 1)
Application.EnableEvents = True
End If
End Sub

Posted by Ivan F Moala on December 25, 2001 4:35 AM

Here is one way to do it;

Private Sub TextBox1_Change()
Dim Count As Integer
Dim Char As String
Static PrevText As String

With TextBox1
If Not IsNumeric(TextBox1.Text) Then
.Text = PrevText & Chr(10)
If Len(.Text) > 1 Then .SelStart = Len(.Text) - 1
End If
For Count = 1 To Len(.Text)
Char = Mid$(.Text, Count, 1)

Select Case Char
Case "0" To "9", ".", vbCrLf
'Allow 0 - 9 + . + vbcrlf
Case Else
Beep
.Text = PrevText
.SelStart = Count - 1
End Select
Next Count
PrevText = .Text
End With
End Sub

Ivan



Posted by WangYa on December 26, 2001 8:48 PM

Works but how to delete the last digit?


Ivans thing works as well but when you try to delete the nomber in the TextBox you can do so but the last digit always stays there. How to empty the Textbox?