Numeric only input for textbox

Barbados

Registered User.
Local time
Today, 13:53
Joined
May 22, 2013
Messages
27
Hi,

I want numeric only input for a certain textbox. I can use a ISNUMERIC function to test this, but prefer to use the Keypress event. So far got the following:

Code:
Private Sub txt_Position_KeyPress(KeyAscii As Integer)
    Select Case KeyAscii
        Case 8  ' backspace
        Case Asc("1") To Asc("9")
        Case Asc("-")
            If InStr(1, Me.txt_Position.Text, "-") > 0 Or Me.txt_Position.SelStart > 0 Then
                KeyAscii = 0
            End If
        Case Asc(".")
            If InStr(1, Me.txt_Position.Text, ".") > 0 Then
                KeyAscii = 0
            End If
        Case Asc("0")
            [COLOR=red]' What do I do here?[/COLOR]
        Case Else
            KeyAscii = 0
    End Select
End Sub

It is working alright, except I want to avoid input that has multiple leading zeros, such as 00000 or -00000 (i.e. minus sign with mulitple leading zeros). Anyone knows how to catch this?

Thanks
 
How about this in On Key Press:

Code:
'Accept numerals only.
    If KeyAscii < 48 Or KeyAscii > 57 Then KeyAscii = 0

Catalina
 
You'll have t handle that elsewhere; does this work for you:
Code:
Private Sub TextboxName_AfterUpdate()
 Me.TextboxName = Abs(Me.TextboxName)
End Sub

Also, your current code doesn't allow for users to use things like the Delete, Backspace, Return/Enter, Right and Left Arrow Keys, and the Tab Key . These are all routinely used to navigate within a Textbox.

Linq ;0)>
 
I'm sure you have an excellent reason for wanting to use a couple of dozen lines of code when one would do.
 
Guys,

What I want is the following:

If the fist character the user types in that textbox is 0, then his second can't be a 0, since 00 is not gonna give me any number.

Likewise, if the first character is - (minus sign) and second character is a 0, then third character can't be a zero.

And of course, the user should be able to enter numbers like 100 and so on.
 
Which line will do it?

Actually, you don't even need a line. Just go into the Properties box, Format Tab, specify any of the available numeric formats and you are done.

Best,
Jiri
 
I want numeric only input for a certain textbox. I can use a ISNUMERIC function to test this, but prefer to use the Keypress event.
Why would you write your own code if there is a function that will provide the solution? Or as Solo suggested, a property setting.
 
Didn't mean to rain on your parade but to be effective with Access, it is best to go with the flow and use the built-in functions and properties and only write your own code only where custom code is required. Since all you care about is if something is numeric, either the property setting or the IsNumeric() function would determine that. The function has a slight advantage in that you can customize the error message you display. The property settings will display Access' internal error and you can't change it.
 
One thing to watch with testing for numeric is that it will accept exponential notation.

These expression both return True:

IsNumeric("2E4")
IsNumeric("3D7")
 

Users who are viewing this thread

Back
Top Bottom