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

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
adamkushner
Asked:
adamkushner
1 Solution
 
aeklundCommented:
You could print all ascii values and characters...

Dim i as integer
For i = 14 to 255
  Debug.print i, chr(i)
next
0
 
aeklundCommented:
or if you want a website, here is one that lists them all:

http://www.asciitable.com/
0
 
christopher_smithCommented:
0
Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

 
DocMCommented:
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
 
BWarmuskerkenCommented:
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
 
spauljosephCommented:
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
 
adamkushnerAuthor Commented:
Seems to work perfect, thanks.
0
 
adamkushnerAuthor Commented:
0

Featured Post

The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

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