To force the user to stay in the box until 8 digits are entered, use the
BeforeUpdate event of the text box.
To prevent the box accepting anything other than a digit, use the KeyPress
event of the text box.
These examples assume the text box is named "MyBox", and you have set the
two propeties to:
[Event Procedure]
Private Sub MyBox_BeforeUpdate(Cancel As Integer)
With Me.MyBox
If Not IsNull(.Value) then
If Len(.Text) <> 8 Then
Cancel = True
MsgBox "8 digits required."
Else
If Not IsNumeric(.Value) Then
Cancel = True
MsgBox "Numbers only."
End If
End If
End If
End With
End Sub
Private Sub MyBox_KeyPress(KeyAscii As Integer)
If KeyAscii < 48 Or KeyAscii > 57 Then
If KeyAscii <> vbKeyBack Then
KeyAscii = 0
End If
End If
End Sub
--
Allen Browne - Microsoft MVP. Perth, Western Australia.
Reply to group, rather than allenbrowne at mvps dot org.
Ezekiël said:
Could someone help me with a code to check if an entry in a textbox is 8
numbers or not.
Something like if it is a number or not then messagebox. Then if it is a
number check if it has 8 digits, if not messagebox.
Greetings,
Ezekiel