1) Use an up-down control with minimum = 0 and maximum = 9
OR
2) A non-editable combobox with values preset in range o to 9.
Therefore the user can *only* choose a value within your permitted range - o
additional validation, etc. required.
HTH
Rob.
<~ ToplessDancer ~> wrote in message
news:a2rievs1hf3c285b8...@4ax.com...
Private Sub Text1_Change()
If Instr("0123456789",Text1.Text) < 1 Then
Text1.Text=""
Text1.Refresh
End If
End Sub
<~ ToplessDancer ~> wrote in message
news:a2rievs1hf3c285b8...@4ax.com...
you can do the trick like this:
Private Sub Text1_KeyPress(keyascii As Integer)
If Not ((keyascii >= 48 and keyascii <= 57) or keyascii = 8 or
keyascii = 9) Then
keyascii = 0
Msgbox "Please enter numbers only"
End If
'keyascii = 0 makes VB ignore the current key press event.
'(keyascii >= 48 and keyascii <= 57): Numbers from 0 to 9
'keyascii = 8: Backspace
'keyascii = 9: Tab
'If you really want to clear the field then replace the "keyascii = 0"
with Text1.text=""
End Sub
You can either use the Change event, which occurs every time the
content of the text box is changed.
Or you use the validate event which occurs before the user leaves the
textbox. It all depends on whether the user should get a chance to
correct his error before you correct him.
What you can do:
Remember the last, valid value entered in the text box. Check in
either the Change or Validate event. If the check goes OK, remember
that value, if not, restore the last valid value and tell the user
what he did wrong.
Robert
Another way of going about it (and arguably the simplest to
understand) is to do the following:
*Set the max character length in the text box to 1
*add the following statement to the textbox coding:
'/-----------------------------------\
'| Numeric restriction |
'\-----------------------------------/
If not IsNumeric Then
MsgBox ("Please enter Numbers only", vbCritical)
Me.Text1.text = ""
Me.Text1.SetFocus
End If