Wrox Programmer Forums
|
Access VBA Discuss using VBA for Access programming.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Access VBA section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old February 28th, 2006, 09:02 PM
Authorized User
 
Join Date: Feb 2006
Posts: 33
Thanks: 0
Thanked 0 Times in 0 Posts
Default Determine if numeric

Is there way to determine if the contents of a text box are numeric. I need to verify that all the entries into the text box are numeric only and letters or symbols.

Thanks,
Scott
__________________
ScottP
 
Old February 28th, 2006, 11:28 PM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 1,093
Thanks: 1
Thanked 12 Times in 11 Posts
Default

Hi Scott,

Here's two control-level data validation routines that will force numeric-only input into a textbox. Non-numeric characters simply can't be entered into the control:

A textbox control's KeyPress event takes one argument, KeyAscii, that returns the ACSII value of the key pressed.

Private Sub Text0_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

    ' Chr$ pulls character value from ASCII value, which
    ' is then evaluated as a number. If value is not numeric,
    ' KeyAscii value set to 0, which prevents any text from
    ' being entered.
    If Not IsNumeric(Chr$(KeyAscii)) Then KeyAscii = 0

End Sub


Private Sub Text0_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

    ' Block the keypress if it is not a digit by comparing the
    ' KeyAscii value with the ASCII values of the numeric
    ' characters (ASCII - 48 through 57).
    If (KeyAscii < 48) Or (KeyAscii > 57) Then KeyAscii = 0
End Sub


HTH,

Bob


 
Old March 1st, 2006, 09:36 AM
Friend of Wrox
 
Join Date: Mar 2004
Posts: 3,069
Thanks: 0
Thanked 10 Times in 10 Posts
Default

You can also enforce data validation at the table level.



mmcdonal
 
Old March 1st, 2006, 12:59 PM
Authorized User
 
Join Date: Feb 2006
Posts: 33
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Thanks Bob, your code worked exactly as I wanted it.
Thanks mmcdonal, I used you suggestion in a few other places in my database.

Scott
 
Old March 2nd, 2006, 08:44 AM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 1,151
Thanks: 2
Thanked 14 Times in 14 Posts
Send a message via ICQ to SerranoG Send a message via AIM to SerranoG
Default

There is also the IsNumeric() function.

Greg Serrano
Michigan Dept. of Environmental Quality, Air Quality Division





Similar Threads
Thread Thread Starter Forum Replies Last Post
From numeric to alfa numeric ebekir XSLT 1 August 10th, 2007 06:13 AM
Determine Credentials bmumph C# 2 November 1st, 2005 12:18 PM
Determine OS adman Beginning VB 6 2 January 5th, 2004 02:26 AM
Need Help: Can't determine cause of error xgbnow Visual C++ 3 September 22nd, 2003 05:00 PM
Determine if something has already been selected harpua Classic ASP Basics 1 June 13th, 2003 01:02 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.