How to solve this problem

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • muddasirmunir
    Contributor
    • Jan 2007
    • 284

    How to solve this problem

    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

    Code:
     
    Private Sub amounttxt_KeyPress(KeyAscii As Integer)
    Dim comp As Boolean
    comp = Chr(KeyAscii) Like "[0-9.]"
    If Chr(KeyAscii) = vbBack Then Exit Sub
    If comp = False Then
    KeyAscii = 0
    End If
    End Sub
    the code is working fine the problem arises
    when the user mistaken writes some values like this

    Code:
     200.62.365 instead of 20062.365
    or 
    20..36 instead of 20.36 
     
    (by mistake user may enter two time decimal in the box )
    as i am also doing some more calculations in it so it give error .

    so how to avoid this . what is the solution?
    thanks
  • mafaisal
    New Member
    • Sep 2007
    • 142

    #2
    Hello Nasir

    Try This

    Code:
    Private Sub Text1_KeyPress(KeyAscii As Integer)
    Dim TmpStr As String
    TmpStr = "0123456789."
    If KeyAscii = 8 Then Exit Sub
    If InStr(TmpStr, Chr(KeyAscii)) = 0 Then KeyAscii = 0
    Dim Tmp As Integer
    Tmp = InStr(Text1.Text, ".")
    If Tmp > 0 And KeyAscii = 46 Then KeyAscii = 0
    End Sub
    I Think This Solve ur Problem

    Faisal

    Comment

    • kadghar
      Recognized Expert Top Contributor
      • Apr 2007
      • 1302

      #3
      I'd rather use the ISNUMERIC function.

      it does the same thing:
      [CODE=vb]
      Private Sub TextBox1_KeyPre ss(ByVal KeyAscii As MSForms.ReturnI nteger)
      If (KeyAscii = 8 Or KeyAscii = 45) Then Exit Sub
      If Not IsNumeric(TextB ox1.Text & Chr(KeyAscii)) Then KeyAscii = 0
      End Sub[/CODE]

      HTH

      Now that i read my code, you should check that the Ascii 45 only is valid when len(textbox1.te xt) = 0
      Last edited by kadghar; Jun 23 '08, 03:26 PM. Reason: add a note

      Comment

      • muddasirmunir
        Contributor
        • Jan 2007
        • 284

        #4
        i think isnumeric function is much easier

        Comment

        • muddasirmunir
          Contributor
          • Jan 2007
          • 284

          #5
          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(TextB ox1.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

          Comment

          • kadghar
            Recognized Expert Top Contributor
            • Apr 2007
            • 1302

            #6
            well,

            something like this will do:

            [CODE=vb]If Not IsNumeric(TextB ox1.Text & Chr(KeyAscii)) Or KeyAscii = 45 Or KeyAscii = 43 Then KeyAscii = 0[/CODE]

            HTH

            Comment

            Working...