Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

ReEnter a number

2 views
Skip to first unread message

~_toplessdancer_~

unread,
Jun 13, 2003, 2:19:43 AM6/13/03
to
I have a Text box where i want someone to enter a number from 0 - 9
and if they enter a number other than 0-9, then how can i clear the
text box and make them re-enter a number again?

Rob Strover

unread,
Jun 13, 2003, 4:38:46 AM6/13/03
to
If you only want the user to enter the single digit the easiest way is not
to use a textbox, but some other way. Here are a couple of alternative
approaches:

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...

CajunCoiler (http://www.cajuncoiler.tk)

unread,
Jun 13, 2003, 9:40:38 AM6/13/03
to
Assuming your text box is called Text1, here's what you'll need...

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...

HKSHK

unread,
Jun 25, 2003, 2:05:10 AM6/25/03
to
Hello ToplessDancer,

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

Asperamanca

unread,
Jun 25, 2003, 9:09:52 AM6/25/03
to
~ ToplessDancer ~ wrote in message news:<a2rievs1hf3c285b8...@4ax.com>...
> I have a Text box where i want someone to enter a number from 0 - 9
> and if they enter a number other than 0-9, then how can i clear the
> text box and make them re-enter a number again?

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

Michael Warner

unread,
Mar 20, 2005, 10:50:26 AM3/20/05
to
Hey topless,

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

0 new messages