?
Solved

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

Posted on 2003-03-07
8
Medium Priority
?
303 Views
Last Modified: 2008-02-01
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
Comment
Question by:adamkushner
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
8 Comments
 
LVL 10

Expert Comment

by:aeklund
ID: 8091146
You could print all ascii values and characters...

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

Expert Comment

by:aeklund
ID: 8091152
or if you want a website, here is one that lists them all:

http://www.asciitable.com/
0
 
LVL 2

Expert Comment

by:christopher_smith
ID: 8091164
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 3

Expert Comment

by:DocM
ID: 8092103
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
 
LVL 1

Expert Comment

by:BWarmuskerken
ID: 8092748
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
 
LVL 2

Accepted Solution

by:
spauljoseph earned 200 total points
ID: 8093546
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 Comment

by:adamkushner
ID: 8093887
Seems to work perfect, thanks.
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Most everyone who has done any programming in VB6 knows that you can do something in code like Debug.Print MyVar and that when the program runs from the IDE, the value of MyVar will be displayed in the Immediate Window. Less well known is Debug.Asse…
I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…
Suggested Courses
Course of the Month9 days, 6 hours left to enroll

764 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question