By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,946 Members | 734 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,946 IT Pros & Developers. It's quick & easy.

How to solve this problem

100+
P: 284
i am using vb6.

in a form i had a text box where i only want to accept numeric values
it may be decimal value
so i use the following code in key press event of the textbox

Expand|Select|Wrap|Line Numbers
  1.  
  2. Private Sub amounttxt_KeyPress(KeyAscii As Integer)
  3. Dim comp As Boolean
  4. comp = Chr(KeyAscii) Like "[0-9.]"
  5. If Chr(KeyAscii) = vbBack Then Exit Sub
  6. If comp = False Then
  7. KeyAscii = 0
  8. End If
  9. End Sub
the code is working fine the problem arises
when the user mistaken writes some values like this

Expand|Select|Wrap|Line Numbers
  1.  200.62.365 instead of 20062.365
  2. or 
  3. 20..36 instead of 20.36 
  4.  
  5. (by mistake user may enter two time decimal in the box )
  6.  
  7.  
as i am also doing some more calculations in it so it give error .

so how to avoid this . what is the solution?
thanks
Jun 23 '08 #1
Share this Question
Share on Google+
5 Replies


mafaisal
100+
P: 142
Hello Nasir

Try This

Expand|Select|Wrap|Line Numbers
  1. Private Sub Text1_KeyPress(KeyAscii As Integer)
  2. Dim TmpStr As String
  3. TmpStr = "0123456789."
  4. If KeyAscii = 8 Then Exit Sub
  5. If InStr(TmpStr, Chr(KeyAscii)) = 0 Then KeyAscii = 0
  6. Dim Tmp As Integer
  7. Tmp = InStr(Text1.Text, ".")
  8. If Tmp > 0 And KeyAscii = 46 Then KeyAscii = 0
  9. End Sub
  10.  
  11.  
  12.  
I Think This Solve ur Problem

Faisal
Jun 23 '08 #2

kadghar
Expert 100+
P: 1,295
I'd rather use the ISNUMERIC function.

it does the same thing:
Expand|Select|Wrap|Line Numbers
  1. Private Sub TextBox1_KeyPress(ByVal KeyAscii As MSForms.ReturnInteger)
  2. If (KeyAscii = 8 Or KeyAscii = 45) Then Exit Sub
  3. If Not IsNumeric(TextBox1.Text & Chr(KeyAscii)) Then KeyAscii = 0
  4. End Sub
HTH

Now that i read my code, you should check that the Ascii 45 only is valid when len(textbox1.text) = 0
Jun 23 '08 #3

100+
P: 284
i think isnumeric function is much easier
Jun 23 '08 #4

100+
P: 284
Hy

i haved used this
  1. <LI style="FONT-SIZE: 8pt; BACKGROUND: #fcfcfc">[font='Courier New', Courier, monospace]If (KeyAscii = 8 Or KeyAscii = 45) Then Exit Sub[/font]
  2. [font='Courier New', Courier, monospace]If Not IsNumeric(TextBox1.Text & Chr(KeyAscii)) Then KeyAscii = 0[/font]
but this is acceptiong + and - also (i-e +2336.36 or -36693)
i had reduced the keyascii codes and try till 35 but still its accpting + but - is remonve.

how can i remonved both
thanks
Jun 23 '08 #5

kadghar
Expert 100+
P: 1,295
well,

something like this will do:

Expand|Select|Wrap|Line Numbers
  1. If Not IsNumeric(TextBox1.Text & Chr(KeyAscii)) Or KeyAscii = 45 Or KeyAscii = 43 Then KeyAscii = 0
HTH
Jun 23 '08 #6

Post your reply

Sign in to post your reply or Sign up for a free account.