Results 1 to 14 of 14

Thread: Simple text box question

  1. #1
    Guest

    Talking

    How can we prevent the user enter some crazy number in text box... i know we can prevent them from non-numeric number... but the user can still enter like "000358" which is not a valid area number or dollar number..... what can i do? they can enter "0.568.254" also..... what should i do?

  2. #2
    Member
    Join Date
    May 2000
    Posts
    41
    i've posted this before but i think it's what you are looking for... (taken from vb5 help file on the 'format' command):

    MyStr = Format(5459.4, "##,##0.00") ' Returns "5,459.40".
    MyStr = Format(334.9, "###0.00") ' Returns "334.90".
    MyStr = Format(5, "0.00%") ' Returns "500.00%".

    MyStr = Format("HELLO", "<") ' Returns "hello".
    MyStr = Format("This is it", ">") ' Returns "THIS IS IT".

    hope this helps
    Here I doeh again

  3. #3
    Guest

    Talking

    Thanks for the hints... i know those also...
    But, what i need is prevent the user to enter invalid number, just like Isnumeric can prevent any non-numeric data... but how can we prevent user to enter "03.5"?

  4. #4
    _______ HeSaidJoe's Avatar
    Join Date
    Jun 1999
    Location
    Canada
    Posts
    3,946

    <?>

    Code:
    Private Sub Text1_KeyPress(KeyAscii As Integer)
     'no decimals allowed
        If KeyAscii = 46 Then KeyAscii = 0
    
    End Sub
    "A myth is not the succession of individual images,
    but an integerated meaningful entity,
    reflecting a distinct aspect of the real world."

    ___ Adolf Jensen

  5. #5
    Member
    Join Date
    May 2000
    Posts
    41
    ok, try this... it should solve both your worries since it only allows integers.. this works with a textbox named text1 change it to the name of whatever the textbox you are using is and handle the error however you see fit:


    Private Sub Text1_LostFocus()
    On Error GoTo not_num
    Text1.Text = Str(Int(Text1.Text))
    Exit Sub
    not_num:
    MsgBox ("got an error")
    End Sub
    Here I doeh again

  6. #6
    Guest

    Talking

    Thanks for all the suggestion.... and i have tried them before...... (by the way, how can i paste the message icon here?)
    The thing is, i allow normal data like 0.21,123, .... but just preventing the crazy number like 02.34, 000000.2 anyone please

  7. #7
    Member
    Join Date
    May 2000
    Posts
    41
    what i just sent you does what you asked. as long as they enter the data and click on something else (like an OK button) then it will trigger the lostfocus code in the text box and it works.
    Here I doeh again

  8. #8
    Hyperactive Member dsy5's Avatar
    Join Date
    Jul 2000
    Location
    Lockport, NY
    Posts
    362
    Not that hard
    Let them enter it. The formatting will take care of it. As for the double decimal points,
    if you use IsNumeric(.345.346) it will return false.

    Code:
    Private Sub Command1_Click()
    Dim t As Variant
    t = Text1
    If IsNumeric(t) Then
        t = Format(t, "#,###,###.00")
        Debug.Print t
    Else
    MsgBox("Not a valid number")
    End If
    Text1.Text = vbNullChar
    End Sub
    Donald Sy - VB (ab)user

  9. #9
    Guest

    Talking

    But i try to prevent them entering crazy number but not have a "check" after it... but i think ur's is best by now Again, how can i have the message icon in the message?

  10. #10
    _______ HeSaidJoe's Avatar
    Join Date
    Jun 1999
    Location
    Canada
    Posts
    3,946

    <?>

    If you have a specify context to what you want to enter then you should use a MaskeditBox and not a text box.

    If..if you entry is always 3 numbers a comma 3 numbers a decial and 2 numbers.
    The maskeditbox mask would be:
    MaskEdBox1.Mask = "###,###.00"

    That way a user must enter number number number comma
    number number number decimal number number

    If not sure how to use one then add one to your form, select it and press F1


    "A myth is not the succession of individual images,
    but an integerated meaningful entity,
    reflecting a distinct aspect of the real world."

    ___ Adolf Jensen

  11. #11
    Guest

    Talking

    dsy5, if i do what you said, what would be the number after this 00000.5? will it returns 0.5 or "invalid number"? thanks again

  12. #12
    Hyperactive Member dsy5's Avatar
    Join Date
    Jul 2000
    Location
    Lockport, NY
    Posts
    362
    It would return .50.
    Clip the code and try it.
    Need a Textbox named text1 (default) and a command button named Command1 (default).
    Donald Sy - VB (ab)user

  13. #13
    Guest

    Talking

    HeSaidJoe, thanks for the reply.....
    but i can not use the masked nox, i thought that before... because i ask people to enter the cost they spend on clothes... it can be 0.000003 to 1000000000000 some crazy people enters 0542.32 make me crazy

  14. #14
    Member
    Join Date
    May 2000
    Posts
    41
    if i understand what you are saying, you could make a label and have it display an error when there is one instead of having a msgbox pop up.

    Here I doeh again

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width