Go Back   Access World Forums > Microsoft Access Discussion > Modules & VBA

 
Reply
 
Thread Tools Rate Thread Display Modes
Old 03-07-2019, 05:11 AM   #1
aman
Newly Registered User
 
Join Date: Oct 2008
Posts: 1,250
Thanks: 54
Thanked 3 Times in 2 Posts
aman is an unknown quantity at this point
Validation on textbox not to accept any alphabets

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 is offline   Reply With Quote
Old 03-07-2019, 05:14 AM   #2
aman
Newly Registered User
 
Join Date: Oct 2008
Posts: 1,250
Thanks: 54
Thanked 3 Times in 2 Posts
aman is an unknown quantity at this point
Re: Validation on textbox not to accept any alphabets

Made it to work. Just add the following line of code instead of the message box:
Code:
KeyAscii = 0
aman is offline   Reply With Quote
Old 03-07-2019, 06:22 AM   #3
bdra2778
Newly Registered User
 
Join Date: Feb 2019
Location: Nicaragua
Posts: 34
Thanks: 6
Thanked 7 Times in 7 Posts
bdra2778 is on a distinguished road
Re: Validation on textbox not to accept any alphabets

Try to use IsNumeric() Function.

bdra2778 is offline   Reply With Quote
Old 03-07-2019, 08:56 AM   #4
Mark_
Longboard on the internet
 
Join Date: Sep 2017
Location: Not the middle of no where, but I can see the road to it from my house.
Posts: 2,112
Thanks: 20
Thanked 400 Times in 393 Posts
Mark_ will become famous soon enough Mark_ will become famous soon enough
Re: Validation on textbox not to accept any alphabets

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.
Mark_ is offline   Reply With Quote
Old 03-11-2019, 08:19 AM   #5
apr pillai
Newly Registered User
 
apr pillai's Avatar
 
Join Date: Jan 2005
Location: India
Posts: 686
Thanks: 3
Thanked 109 Times in 96 Posts
apr pillai will become famous soon enough
Send a message via AIM to apr pillai Send a message via Yahoo to apr pillai Send a message via Skype™ to apr pillai
Re: Validation on textbox not to accept any alphabets

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

To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
(Learn MS-Access Tips and Tricks)

All responses are based on Access2003/2007
apr pillai is offline   Reply With Quote
Old 03-11-2019, 04:17 PM   #6
Galaxiom
Super Moderator
 
Join Date: Jan 2009
Location: NSW Australia
Posts: 11,717
Thanks: 105
Thanked 1,511 Times in 1,424 Posts
Galaxiom is a splendid one to behold Galaxiom is a splendid one to behold Galaxiom is a splendid one to behold Galaxiom is a splendid one to behold Galaxiom is a splendid one to behold Galaxiom is a splendid one to behold Galaxiom is a splendid one to behold
Re: Validation on textbox not to accept any alphabets

Quote:
Originally Posted by bdra2778 View Post
Try to use IsNumeric() Function.
That suggestion does not meet the requested requirements.
Code:
IsNumeric("-12,,34,5.6E7") = True

Galaxiom is offline   Reply With Quote
Reply

Thread Tools
Display Modes Rate This Thread
Rate This Thread:

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Password validation to accept both upper and lowercase text Fmeister Modules & VBA 7 04-06-2011 07:05 AM
Validation to not accept numerical characters Zholt Tables 3 02-20-2010 05:45 PM
How to make textbox accept only P,R,I,D,E characters? swaroop1012 Forms 17 07-23-2009 08:04 AM
Search by alphabets or numbers alkindy General 3 09-05-2007 01:28 PM
Validation Rule to accept Alphabetical Characters Module Problem faizulhu Modules & VBA 3 10-30-2006 07:07 AM




All times are GMT -8. The time now is 10:48 PM.


Microsoft Access Help
General
Tables
Queries
Forms
Reports
Macros
Modules & VBA
Theory & Practice
Access FAQs
Code Repository
Sample Databases
Video Tutorials

Featured Forum post


Sponsored Links


Powered by vBulletin®
Copyright ©2000 - 2019, Jelsoft Enterprises Ltd.
(c) copyright 2017 Access World