Textbox limit to numbers issue

GraemeG

Registered User.
Local time
Today, 09:55
Joined
Jan 22, 2011
Messages
212
Hello.

I have the following code on Keypress of a textbox. to limit the repsonse to just numbers. Which works great!
However I need it to only allow four digits and be able to use the backspace key to delete.
But it allows any length of number and also wont allow the backspace key to delete.
Any help please! Thanks.

Code:
Private Sub txtEntranceDoorsFlatsRenewYear_KeyPress(KeyAscii As Integer)
Const Number$ = "0123456789" ' only allow these characters
If KeyAscii <> 4 Then
   If InStr(Number$, Chr(KeyAscii)) = 0 Then
      KeyAscii = 0
      Exit Sub
   End If
End If
End Sub
 
Not sure I understand but if you are checking for a maximum number of characters being entered then If keyAsscii <> 4 doesn't look right.

Have you tried using the LEN function?
 
Not sure I understand but if you are checking for a maximum number of characters being entered then If keyAsscii <> 4 doesn't look right.

Have you tried using the LEN function?

Yeah I have a textbox. But I want to limit the response of the user to just numbers with a set length of 4 otherwise invalid.
I thought the esaiest way was to use ascii on keypress so no other key enters anything other than numberpad. This works great because a user could never enter anything other than a number. However I do not know how to set the length to <> 4. But what i have found using this ascii keypress is that the Backspace (delete) button no longer works which might get on the users neveres a little.

Do you have any suggestions?
 
Lots of ways to handle this. I may have used an InputBox, followed by an isNumeric check and also a further check that examines the content of the input box

If myeMail Like "*[!0-9a-Z@._-]*" = False - this code checks for a valid eMail so if you used just the 10-9 part it soulr ensure only number are entered.
 
Lots of ways to handle this. I may have used an InputBox, followed by an isNumeric check and also a further check that examines the content of the input box

If myeMail Like "*[!0-9a-Z@._-]*" = False - this code checks for a valid eMail so if you used just the 10-9 part it soulr ensure only number are entered.

All the forms are setup now with textboxes. is there no code to insert that would limit the length of an entry?
 
Two things - one is to check the input of the text box with the LEN function and throw a message boox if they enter more than 4 but IF the textbox is bound to a table, then just set the field size to 4
 
Two things - one is to check the input of the text box with the LEN function and throw a message boox if they enter more than 4 but IF the textbox is bound to a table, then just set the field size to 4

haha deary me!
Yep I have set the field size in the bound table and that has now worked!
Thanks.

Do you have any ideas on the use of backspace and ascii keypress code?
 
RESOLVED!
After editing the field length! The following code now works:
Code:
Private Sub txtEntranceDoorsFlatsRenewYear_KeyPress(KeyAscii As Integer)
    'If a backspace (ASCII-8) or a tab (ASCII-9) was
    ' entered, allow it unconditionally.
    If (KeyAscii = 8) Or (KeyAscii = 9) Then Exit Sub
    If IsNumeric(Chr(KeyAscii)) <> True Then KeyAscii = 0
End Sub
 
Well done. Glad to have helped you think it through. T
 

Users who are viewing this thread

Back
Top Bottom