• Status: Solved
• Priority: Medium
• Security: Public
• Views: 304

# Numeric Charictors Only (in a textbox), but also a "\$", "," and a "."

I have a textbox that accepts a Currency.  I need to only allow the charictors related, i.e. "\$", "." ",".

Here's my code for numeric only,

Private Sub txtPrice_KeyPress(KeyAscii As Integer)
KeyAscii = IIf(Not KeyAscii = 8 And Not IsNumeric(Chr(KeyAscii)), 0, KeyAscii)
End Sub

Also,

Where can I find a list of the ascii values for all keys?

Thanks.
0
1 Solution

Commented:
You could print all ascii values and characters...

Dim i as integer
For i = 14 to 255
Debug.print i, chr(i)
next
0

Commented:
or if you want a website, here is one that lists them all:

http://www.asciitable.com/
0

Commented:
0

Commented:
Here is a possible solution.
Regards.

Private Sub txtPrice_KeyPress(KeyAscii As Integer)

Select Case KeyAscii
Case 8
'Backspace accepted
Case 101, 69 ' Letter E, e
'Scientific notation not accepted
KeyAscii = 0
Case Else
'Test if it is a numeric expression
'Add a zero to accept - and + signs
SelStart = txtPrice.SelStart
If Not IsNumeric(Mid(txtPrice.Text, 1, SelStart) & Chr(KeyAscii) & Mid(txtPrice.Text, SelStart + 1) & "0") Then
KeyAscii = 0
Else
'Accepted
End If
End Select

End Sub
0

Commented:
You don't necessarily need to use the Ascii value, use VBs variables vbKey1, vbKey2, vbKeyBack,

type: vbKey(the control spacebar) for the whole list.
0

Commented:
You can use this

Private Sub txtPrice_KeyPress(KeyAscii as Integer)
if InStr("0123456789\$,.",Chr(KeyAscii))>0 or keyAscii=8 Then
'do Nothing
Else
KeyAscii=0
End If
End Sub
0

Author Commented:
Seems to work perfect, thanks.
0

Author Commented:
0

## Featured Post

Tackle projects and never again get stuck behind a technical roadblock.