473,490 Members | 2,486 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

keyAscii

Hey there, what i want to do is have a text field that can only accept
numbers, full stops and the backspace key.
I currently have the below code to accept only numbers, but no full stops or
backspace.
Im sure this is really simple, and i will be greatful for a respons..

Private Sub txtBal_KeyPress(KeyAscii As Integer)
If KeyAscii < Asc("0") Or KeyAscii > Asc("9") Then
KeyAscii = 0
MsgBox "Please enter only numbers"
End If

End Sub

Jason
Jul 17 '05 #1
6 31942
On Mon, 26 Jul 2004 07:38:18 GMT, margetts wrote:
Hey there, what i want to do is have a text field that can only accept
numbers, full stops and the backspace key.
I currently have the below code to accept only numbers, but no full stops or
backspace.
Im sure this is really simple, and i will be greatful for a respons..

Private Sub txtBal_KeyPress(KeyAscii As Integer)
If KeyAscii < Asc("0") Or KeyAscii > Asc("9") Then
KeyAscii = 0
MsgBox "Please enter only numbers"
End If

End Sub

Jason


Try this instead of the if-then block:
Select Case KeyAscii
Case 8, 13, 48 To 57
'blah!
Case Else
KeyAscii = 0
End Select

--
auric underscore underscore at hotmail dot com
*****
Hi, I'm Auric__, and I'm here to waste your time and bandwidth.
Jul 17 '05 #2

"margetts" <ma******@bigpond.com> wrote in message
news:Kx******************@news-server.bigpond.net.au...
Hey there, what i want to do is have a text field that can only accept
numbers, full stops and the backspace key.
I currently have the below code to accept only numbers, but no full stops or backspace.
Im sure this is really simple, and i will be greatful for a respons..

Private Sub txtBal_KeyPress(KeyAscii As Integer)
If KeyAscii < Asc("0") Or KeyAscii > Asc("9") Then
KeyAscii = 0
MsgBox "Please enter only numbers"
End If

End Sub

Jason


You might want to try using select instead of if

Select Case KeyAscii
Case 8 'backspace
Case 46 'full stop
Case 48 to 57 'range of numbers
Case else
MsgBox "Please enter only numbers"
KeyAscii = 0
End Select

you could put MsgBox KeyAscii under the Case else line, then press various
keys to work out which key produces which ascii code.

Steve
Jul 17 '05 #3
On Mon, 26 Jul 2004 00:54:43 -0700, Auric__ wrote:
On Mon, 26 Jul 2004 07:38:18 GMT, margetts wrote:
Hey there, what i want to do is have a text field that can only accept
numbers, full stops and the backspace key.
I currently have the below code to accept only numbers, but no full stops or
backspace.
Im sure this is really simple, and i will be greatful for a respons..

Private Sub txtBal_KeyPress(KeyAscii As Integer)
If KeyAscii < Asc("0") Or KeyAscii > Asc("9") Then
KeyAscii = 0
MsgBox "Please enter only numbers"
End If

End Sub

Jason


Try this instead of the if-then block:
Select Case KeyAscii
Case 8, 13, 48 To 57
'blah!
Case Else
KeyAscii = 0
End Select


Urgh... I read "full stop" as "new line", sorry. Stephen has the right
numbers. :/
--
auric underscore underscore at hotmail dot com
*****
"I..." he says, as he crosses the cpu limit of his attention span.
Jul 17 '05 #4
> Hey there, what i want to do is have a text field that can only accept
numbers, full stops and the backspace key.
I currently have the below code to accept only numbers, but no full stops or backspace.
Im sure this is really simple, and i will be greatful for a respons..

Private Sub txtBal_KeyPress(KeyAscii As Integer)
If KeyAscii < Asc("0") Or KeyAscii > Asc("9") Then
KeyAscii = 0
MsgBox "Please enter only numbers"
End If

End Sub


There is a problem with trying to do what you asked in the KeyPress
event only... the user will be able to Paste bad data into the TextBox.
Here are some solutions which I've posted in the past (there is code
below for both, entries with digits only and for entries with decimal
points). The routines work quite well and protects the TextBox from
pasting non-numeric entries (the user can paste valid data though) as
well as stopping non-numeric keypresses.

Rick - MVP

For typing digits only in the TextBox
=====================================
Dim LastPosition As Long

Private Sub Text1_Change()
Static LastText As String
Static SecondTime As Boolean
If Not SecondTime Then
With Text1
If .Text Like "*[!0-9]*" Then
Beep
SecondTime = True
.Text = LastText
.SelStart = LastPosition
Else
LastText = .Text
End If
End With
End If
SecondTime = False
End Sub

Private Sub Text1_MouseDown(Button As Integer, _
Shift As Integer, X As Single, Y As Single)
With Text1
LastPosition = .SelStart
'Place any other MouseDown event code here
End With
End Sub

Private Sub Text1_KeyPress(KeyAscii As Integer)
With Text1
LastPosition = .SelStart
'Place any other KeyPress checking code here
End With
End Sub
For typing floating point numbers in the TextBox
=========================================
' Set the maximum number of digits before the
' decimal point in the MaxWhole constant. Set
' the maximum number of digits after the decimal
' point in the MaxDecimal constant.
Dim LastPosition As Long

Private Sub Text1_Change()
Static LastText As String
Static SecondTime As Boolean
Const MaxDecimal As Integer = 4
Const MaxWhole As Integer = 2
With Text1
If Not SecondTime Then
If .Text Like "*[!0-9.]*" Or _
.Text Like "*.*.*" Or _
.Text Like "*." & String$(1 + MaxDecimal, "#") Or _
.Text Like String$(MaxWhole, "#") & "[!.]" Then
Beep
SecondTime = True
.Text = LastText
.SelStart = LastPosition
Else
LastText = .Text
End If
End If
End With
SecondTime = False
End Sub

Private Sub Text1_MouseDown(Button As Integer, _
Shift As Integer, X As Single, Y As Single)
With Text1
LastPosition = .SelStart
'Place any other MouseDown event code here
End With
End Sub

Private Sub Text1_KeyPress(KeyAscii As Integer)
With Text1
LastPosition = .SelStart
'Place any other KeyPress checking code here
End With
End Sub

Note that you will have check for the Text property containing a single
character consisting of a decimal point since that must be allowed as a
starting character. If you want to allow negative, as well as positive
values, then use this If statement in place of the second If statement
in the Text1_Change event code above:

If .Text Like "*[!0-9.+-]*" Or _
.Text Like "*.*.*" Or _
.Text Like "*." & String$(1 + MaxDecimal, "#") Or _
.Text Like "*" & String$(MaxWhole, "#") & "[!.]" Or _
.Text Like "?*[+-]*" Then

Note that now you will have to check the Text property for this one to
see if it contains a single plus, minus or decimal point.

I guess I should mention that I'm in the US where the decimal point is a
"dot". If your decimal point is some other characters, then make the
obvious substitutions in the If-Then tests above; or you could query the
system for the decimal point character, store it in a variable and
concatenate that into the string values above in place of the decimal
point ("dot") that I show above. In keeping with the non-APIness of this
solution, here is what I use to get the system's decimal point.

DecimalPointSymbol = Format$(0, ".")

Jul 17 '05 #5
On Mon, 26 Jul 2004 07:38:18 GMT, "margetts" <ma******@bigpond.com>
wrote:
Hey there, what i want to do is have a text field that can only accept
numbers, full stops and the backspace key.
I currently have the below code to accept only numbers, but no full stops or
backspace.
Im sure this is really simple, and i will be greatful for a respons..

Private Sub txtBal_KeyPress(KeyAscii As Integer)
If KeyAscii < Asc("0") Or KeyAscii > Asc("9") Then
KeyAscii = 0
MsgBox "Please enter only numbers"
End If

End Sub


Private Sub Text1_KeyPress(KeyAscii As Integer)
Dim GoodKeys As String
GoodKeys = "0123456789." + Chr$(127) + Chr$(8)

If InStr(GoodKeys, Chr$(KeyAscii)) = 0 Then
KeyAscii = 0
End If
End Sub

Remember this will not prevent idiots pasting rubbish into your
textbox
Jul 17 '05 #6
This is best
Select case keyascii

then set keyascii to 0 to cancel a keystroke you don't want to pass through
"Stephen Williams" <st*****@hotmail.com> wrote in message
news:RQ*****************@news02.tsnz.net...

"margetts" <ma******@bigpond.com> wrote in message
news:Kx******************@news-server.bigpond.net.au...
Hey there, what i want to do is have a text field that can only accept
numbers, full stops and the backspace key.
I currently have the below code to accept only numbers, but no full
stops or
backspace.
Im sure this is really simple, and i will be greatful for a respons..

Private Sub txtBal_KeyPress(KeyAscii As Integer)
If KeyAscii < Asc("0") Or KeyAscii > Asc("9") Then
KeyAscii = 0
MsgBox "Please enter only numbers"
End If

End Sub

Jason


You might want to try using select instead of if

Select Case KeyAscii
Case 8 'backspace
Case 46 'full stop
Case 48 to 57 'range of numbers
Case else
MsgBox "Please enter only numbers"
KeyAscii = 0
End Select

you could put MsgBox KeyAscii under the Case else line, then press various
keys to work out which key produces which ascii code.

Steve

---
Outgoing mail is certified Virus Free.
Checked by AVG anti-virus system (http://www.grisoft.com).
Version: 6.0.721 / Virus Database: 477 - Release Date: 7/16/2004
Jul 17 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
5405
by: laurenq uantrell | last post by:
In a field's OnKeyPress properties I can execute code that reveals the Ascii key that is pressed: On Key Press: Private Sub FieldName_KeyPress(KeyAscii As Integer) Dim x as Integer If...
1
3140
by: laurenq uantrell | last post by:
In a field's OnKeyPress properties I can execute code that reveals the Ascii key that is pressed: On Key Press: Private Sub FieldName_KeyPress(KeyAscii As Integer) Dim x as Integer If...
2
2180
by: ahmedazzam | last post by:
I need to know Keyascii for different keys
4
16388
by: HelloWorldHelloWorldHello | last post by:
What i want to know is What is the KeyAscii of " "Tab" key" ?.
1
1401
by: HelloWorldHelloWorldHello | last post by:
Can Anyone Tell Me How to direct KeyAscii with using CommandButton Only.?
1
1658
by: HelloWorldHelloWorldHello | last post by:
How to use KeyAscii without any KeyPress(text1_Keypress).? But CommandButton can direct the KeyAscii only.
1
2431
by: Asad Asad | last post by:
Please tell me what is the KeyAscii value of Window Key
3
2073
by: shona | last post by:
Hi, I done program in that i don't want backspace button active in that case i done coading it is working fine in textbox & now i want to use in richtextbox but how? following is my code ...
7
9850
by: raghunadhs | last post by:
with help of key press evnt i am able to find the KeyAscii values.. but i am unable to find the ascii values of "delete","home","pageUp", ...etc.. In my application there is a need to find...
0
6974
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7183
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
5448
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
4878
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4573
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3074
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1389
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
628
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
277
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.