Validation on textbox not to accept any alphabets (1 Viewer)

aman

Registered User.
Local time
Yesterday, 21:31
Joined
Oct 16, 2008
Messages
1,250
Hi guys,

The following code checks if any alphabets are entered in the textbox and display an error message if the user is typing any alphabet. I want to amend this so that no error message displays on the screen but if the user types alphabet then just ignore the alphabet (textbox must not accept any alphabet).

Code:
Private Sub txtAccount1_KeyPress(ByVal KeyAscii As MSForms.ReturnInteger)
 If -CLng(Chr(KeyAscii) Like "[0-9]") = 0 Then MsgBox "Please Enter Numbers Only", vbCritical + vbOKOnly, "Account no validation"
    KeyAscii = KeyAscii * -CLng(Chr(KeyAscii) Like "[0-9]")
End Sub

I hope it makes sense. Any help will be much appreciated.

Thanks
 

aman

Registered User.
Local time
Yesterday, 21:31
Joined
Oct 16, 2008
Messages
1,250
Made it to work. Just add the following line of code instead of the message box:
Code:
KeyAscii = 0
 

bdra2778

Registered User.
Local time
Yesterday, 21:31
Joined
Feb 14, 2019
Messages
34
Try to use IsNumeric() Function.
 

Mark_

Longboard on the internet
Local time
Yesterday, 21:31
Joined
Sep 12, 2017
Messages
2,111
Just as an idea, you could try something more along the lines of

Code:
In your TextBox Before Accept
IF IsNumeric(Trim(Me.TextBox) THEN
   Me.TextBox = TRIM(Me.TextBox)
Else
   Me.TextBox = 'ENTER ACCT NO"
   Me.TextBox.SetFocus
EndIf

This covers when a user decides to cut and past in an account number. With cut and paste you can often get a leading or trailing space. This removes those, checks to see if its a valid account number, then would go about its merry way.

As you are having them enter in an account number I am guessing you are doing other validations while your at it, so this would simply be one of those.

Trying to FORCE them to only press specific keys can cause an issue when uses can cut/paste, especially with the mouse. Best to let them do what they are doing and clean up afterwards since it catches more unusual cases.
 

apr pillai

AWF VIP
Local time
Today, 10:01
Joined
Jan 20, 2005
Messages
735
Try using Input Mask setting, you don't need to use any Code. Try the following:

1. Text Box Input Mask Property setting for 10 digit number: 9999999999
This setting will accept digits 0 to 9 only.

2. Input Mask: ##########
This setting will accept 0 to 9, space, plus or minus signs.
 

Users who are viewing this thread

Top Bottom