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

 
Reply
 
Thread Tools Rate Thread Display Modes
Old 11-09-2019, 02:41 AM   #1
amir0914
Newly Registered User
 
Join Date: May 2018
Posts: 43
Thanks: 49
Thanked 0 Times in 0 Posts
amir0914 is on a distinguished road
only numbers function

Hi guys, I have many text boxes and they should accept only numbers, it means no one can type letters on text box. can someone give me a function to put on text boxes event??

Thanks in advanced.

amir0914 is offline   Reply With Quote
Old 11-09-2019, 03:06 AM   #2
NauticalGent
Pristine Curmudgeon
Gold Supporter
 
NauticalGent's Avatar
 
Join Date: Apr 2015
Location: Naples, Italy (EXPAT from Virginia)
Posts: 2,035
Thanks: 457
Thanked 302 Times in 261 Posts
NauticalGent has a spectacular aura about NauticalGent has a spectacular aura about NauticalGent has a spectacular aura about
Re: only numbers function

Right click on the Text Box and choose properties. On the format tab, change the entry to General Number.

This will give you the default error message. If you want something a little more specific, you will need to add some code.
__________________
It follows then as certain that night succeeds the day, that without normalization, we can do nothing definitive, and with it, everything honorable and glorious. - with apologies to George Washington
NauticalGent is offline   Reply With Quote
The Following User Says Thank You to NauticalGent For This Useful Post:
amir0914 (11-09-2019)
Old 11-09-2019, 04:22 AM   #3
amir0914
Newly Registered User
 
Join Date: May 2018
Posts: 43
Thanks: 49
Thanked 0 Times in 0 Posts
amir0914 is on a distinguished road
Re: only numbers function

Quote:
Originally Posted by NauticalGent View Post
Right click on the Text Box and choose properties. On the format tab, change the entry to General Number.

This will give you the default error message. If you want something a little more specific, you will need to add some code.
I knew this before, is there a way to do this without any massage?? i'm sure is a way with vba code.

amir0914 is offline   Reply With Quote
Old 11-09-2019, 04:25 AM   #4
jdraw
Super Moderator
 
jdraw's Avatar
 
Join Date: Jan 2006
Location: Ottawa, Ontario, Canada;West Palm Beach, Florida
Posts: 12,286
Thanks: 97
Thanked 2,032 Times in 1,979 Posts
jdraw is a glorious beacon of light jdraw is a glorious beacon of light jdraw is a glorious beacon of light jdraw is a glorious beacon of light jdraw is a glorious beacon of light
Re: only numbers function

You could check the textbox with IsNumeric() and take a selected path based on the result of the check. Perhaps the textbox has no value?? You could check that as well.
God luck.
__________________

To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.


To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.


To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
jdraw is offline   Reply With Quote
The Following User Says Thank You to jdraw For This Useful Post:
amir0914 (11-09-2019)
Old 11-09-2019, 06:23 AM   #5
theDBguy
Im here to help
 
theDBguy's Avatar
 
Join Date: Oct 2018
Location: SunnySandyEggo
Posts: 6,656
Thanks: 58
Thanked 1,463 Times in 1,444 Posts
theDBguy has a spectacular aura about theDBguy has a spectacular aura about
Re: only numbers function

Hi. You could try using the KeyPress event to silently reject/ignore non-numeric characters.
__________________
Just my 2 cents...

To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
|
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
|
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
theDBguy is offline   Reply With Quote
The Following User Says Thank You to theDBguy For This Useful Post:
amir0914 (11-09-2019)
Old 11-09-2019, 06:49 AM   #6
missinglinq
AWF VIP
 
missinglinq's Avatar
 
Join Date: Jun 2003
Location: Richmond (Virginia that is!)
Posts: 6,320
Thanks: 11
Thanked 721 Times in 671 Posts
missinglinq is a glorious beacon of light missinglinq is a glorious beacon of light missinglinq is a glorious beacon of light missinglinq is a glorious beacon of light missinglinq is a glorious beacon of light missinglinq is a glorious beacon of light
Re: only numbers function

Here's a hack from the vaults that's the kind of thing that theDBguy suggested...(Replaceing YourTextBoxName with the actual name of your Control.)
Code:
Private Sub YourTextBoxName_KeyDown(KeyCode As Integer, Shift As Integer)

  Select Case KeyCode
  
   Case 48 To 57
   
   'Do nothing: Numerical characters (digits) are allowed
   
   Case vbKeyDelete, vbKeyBack, vbKeyReturn, vbKeyRight, vbKeyLeft, vbKeyTab
   
   'Do nothing: Allow these keys to be used
  
   Case vbKeyNumpad0, vbKeyNumpad1, vbKeyNumpad2, vbKeyNumpad3, vbKeyNumpad4, vbKeyNumpad5, vbKeyNumpad6, vbKeyNumpad7, vbKeyNumpad8, vbKeyNumpad9
   
   'Do nothing: Allow input from Numbers Keypad

   Case Else
   
   'Don't allow anything but digits to be used
	
	KeyCode = 0
  
  End Select

End Sub

Be warned that without a message, some users may think that there keyboard/computer is broken!

Linq ;0)>
__________________
The Devil's in the Details!
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.


(All code solutions tested in Access 2003/2007, before posting, unless otherwise noted.)
missinglinq is offline   Reply With Quote
The Following User Says Thank You to missinglinq For This Useful Post:
MrHans (11-09-2019)
Old 11-09-2019, 06:56 AM   #7
theDBguy
Im here to help
 
theDBguy's Avatar
 
Join Date: Oct 2018
Location: SunnySandyEggo
Posts: 6,656
Thanks: 58
Thanked 1,463 Times in 1,444 Posts
theDBguy has a spectacular aura about theDBguy has a spectacular aura about
Re: only numbers function

Hi Linq. Thanks for the assist. Perhaps use a Beep rather than do nothing? Just a thought...

__________________
Just my 2 cents...

To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
|
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
|
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
theDBguy is offline   Reply With Quote
The Following User Says Thank You to theDBguy For This Useful Post:
amir0914 (11-09-2019)
Old 11-09-2019, 09:43 AM   #8
amir0914
Newly Registered User
 
Join Date: May 2018
Posts: 43
Thanks: 49
Thanked 0 Times in 0 Posts
amir0914 is on a distinguished road
Re: only numbers function

Quote:
Originally Posted by missinglinq View Post
Here's a hack from the vaults that's the kind of thing that theDBguy suggested...(Replaceing YourTextBoxName with the actual name of your Control.)
Code:
Private Sub YourTextBoxName_KeyDown(KeyCode As Integer, Shift As Integer)

  Select Case KeyCode
  
   Case 48 To 57
   
   'Do nothing: Numerical characters (digits) are allowed
   
   Case vbKeyDelete, vbKeyBack, vbKeyReturn, vbKeyRight, vbKeyLeft, vbKeyTab
   
   'Do nothing: Allow these keys to be used
  
   Case vbKeyNumpad0, vbKeyNumpad1, vbKeyNumpad2, vbKeyNumpad3, vbKeyNumpad4, vbKeyNumpad5, vbKeyNumpad6, vbKeyNumpad7, vbKeyNumpad8, vbKeyNumpad9
   
   'Do nothing: Allow input from Numbers Keypad

   Case Else
   
   'Don't allow anything but digits to be used
	
	KeyCode = 0
  
  End Select

End Sub

Be warned that without a message, some users may think that there keyboard/computer is broken!

Linq ;0)>
Thank you buddy, the code was great.
amir0914 is offline   Reply With Quote
Old 11-09-2019, 10:02 AM   #9
isladogs
High Noon Moderator
 
isladogs's Avatar
 
Join Date: Jan 2017
Location: Somerset, UK
Posts: 11,382
Thanks: 115
Thanked 3,114 Times in 2,831 Posts
isladogs is a splendid one to behold isladogs is a splendid one to behold isladogs is a splendid one to behold isladogs is a splendid one to behold isladogs is a splendid one to behold isladogs is a splendid one to behold isladogs is a splendid one to behold
Re: only numbers function

In this code, do nothing means those keys are allowed so you definitely don't want a beep or message.
Where that would be helpful is the Case Else where the key code is remapped to nothing i.e.blocked

Note for others who read this thread in the future.
For that type of key press procedure to work, you must also set KeyPreview =Yes in the form properties
__________________
If this answer has helped, please click the Thanks button and/or click the 'reputation scales' symbol on the left.

Colin (Mendip Data Systems)
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
,
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.


Website links:
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
,
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
,
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
,
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
,
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.


Common sense and a sense of humour are the same thing, moving at different speeds. (Clive James - RIP)
isladogs is offline   Reply With Quote
Old 11-09-2019, 10:28 AM   #10
theDBguy
Im here to help
 
theDBguy's Avatar
 
Join Date: Oct 2018
Location: SunnySandyEggo
Posts: 6,656
Thanks: 58
Thanked 1,463 Times in 1,444 Posts
theDBguy has a spectacular aura about theDBguy has a spectacular aura about
Re: only numbers function

Hi Colin. Thanks for correcting me. I meant to say a Beep might be an option when the key is ignored/discarded. Cheers!
__________________
Just my 2 cents...

To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
|
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
|
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
theDBguy is offline   Reply With Quote
Old 11-09-2019, 11:43 AM   #11
Micron
AWF VIP
 
Join Date: Oct 2018
Location: Ontario, Canada
Posts: 1,253
Thanks: 10
Thanked 232 Times in 220 Posts
Micron has a spectacular aura about Micron has a spectacular aura about
Re: only numbers function

I suppose you could just use numeric values rather than vb constants...
Code:
Select Case KeyCode
    Case 48 To 57
    Case 96 To 105
    Case Else
    Beep
    keycode = 0
End Select
EDIT - nope, that doesn't allow you to tab off. But

Case 9

would.
Just figured out that most/all numerics can go on one line
Code:
Select Case KeyCode
  Case 8, 9, 13, 48 To 57, 96 To 105
  Case Else
  Beep
  KeyCode = 0
End Select
__________________
Sometimes I just roll my eyes out loud...
Windows 10; Office 365 (Access 2016)

Last edited by Micron; 11-09-2019 at 11:54 AM.
Micron is offline   Reply With Quote
Old 11-09-2019, 11:54 AM   #12
missinglinq
AWF VIP
 
missinglinq's Avatar
 
Join Date: Jun 2003
Location: Richmond (Virginia that is!)
Posts: 6,320
Thanks: 11
Thanked 721 Times in 671 Posts
missinglinq is a glorious beacon of light missinglinq is a glorious beacon of light missinglinq is a glorious beacon of light missinglinq is a glorious beacon of light missinglinq is a glorious beacon of light missinglinq is a glorious beacon of light
Re: only numbers function

Quote:
Originally Posted by isladogs View Post

...Note for others who read this thread in the future.
For that type of key press procedure to work, you must also set KeyPreview =Yes in the form properties
...
That's true...but the provided code uses the KeyDown event.

Glad we could help, amir0914!

Good luck with your project!

Linq ;0)>
__________________
The Devil's in the Details!
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.


(All code solutions tested in Access 2003/2007, before posting, unless otherwise noted.)
missinglinq is offline   Reply With Quote
Old 11-09-2019, 12:31 PM   #13
missinglinq
AWF VIP
 
missinglinq's Avatar
 
Join Date: Jun 2003
Location: Richmond (Virginia that is!)
Posts: 6,320
Thanks: 11
Thanked 721 Times in 671 Posts
missinglinq is a glorious beacon of light missinglinq is a glorious beacon of light missinglinq is a glorious beacon of light missinglinq is a glorious beacon of light missinglinq is a glorious beacon of light missinglinq is a glorious beacon of light
Re: only numbers function

Quote:
Originally Posted by Micron View Post

...Just figured out that most/all numerics can go on one line
That's true...but I'm getting old and forgetful...and I like making things as self explanatory as possible!

Linq ;0)>
__________________
The Devil's in the Details!
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.


(All code solutions tested in Access 2003/2007, before posting, unless otherwise noted.)
missinglinq is offline   Reply With Quote
Old 11-09-2019, 12:58 PM   #14
Micron
AWF VIP
 
Join Date: Oct 2018
Location: Ontario, Canada
Posts: 1,253
Thanks: 10
Thanked 232 Times in 220 Posts
Micron has a spectacular aura about Micron has a spectacular aura about
Re: only numbers function

Quote:
That's true...but I'm getting old and forgetful...and I like making things as self explanatory as possible!
Me too; but I'm lazy also...
Besides, there are only so many characters in a keyboard bucket. You don't want to run out, do you?
Micron is offline   Reply With Quote
Old 11-09-2019, 01:02 PM   #15
theDBguy
Im here to help
 
theDBguy's Avatar
 
Join Date: Oct 2018
Location: SunnySandyEggo
Posts: 6,656
Thanks: 58
Thanked 1,463 Times in 1,444 Posts
theDBguy has a spectacular aura about theDBguy has a spectacular aura about
Re: only numbers function

It wasn't specified, but I wonder if decimal point is a valid input. Just curious...

__________________
Just my 2 cents...

To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
|
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
|
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
theDBguy 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
TRIM function for numbers sear100 Reports 4 06-13-2014 04:12 PM
Finding the Average of numbers generated using the IF function Tech_Girl Excel 2 06-11-2013 03:07 AM
Function Val() rounding numbers Ammarhm Modules & VBA 2 09-19-2011 10:38 AM
Function for greater of two numbers access novice Queries 1 08-09-2009 07:48 PM
IIf function in a query mangling numbers Alessandro Queries 7 05-22-2009 05:16 AM




All times are GMT -8. The time now is 01:04 AM.


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