Results 1 to 6 of 6

Thread: [RESOLVED] Need help for input textbox

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Mar 2008
    Posts
    66

    Resolved [RESOLVED] Need help for input textbox

    Hi all,

    I have problem in my VB code.
    I have a text box in GUI for the user to key in the input which is a number,i need to set condition for the input that the user key in. i have write the code as below,

    Private Sub textbox_Change()

    If Val(textbox.Text) < 0.03 Then
    MsgBox "cannot less than 0.03!", vbCritical
    ' reset the invalid number
    textbox.Text = ""
    End If

    End Sub



    1. The problem is when the user enter a number for example 0.05 which should be valid for my condition, but the msgbox pop up immediately when the user just enter the first number of (0.05) which is 0.
    Actually i want the program to verify the number only after the user has finish enter their desire number inside text box.

    2. The text box in my condition is just valid for digit number, if the user enter any input other than a number for example " a, b, c..." then the program will pop up the msg box to warn the user. so what code should i use to add this condition and implement together with my existing condition which is "number input should >0.03 "

    can anyone help me on this? i would really appreaciate for any help given.
    thanks in advance.

  2. #2
    Discovering Life Siddharth Rout's Avatar
    Join Date
    Feb 2005
    Location
    Mumbai, India
    Posts
    12,001

    Re: Need help for input textbox

    if the user enter any input other than a number for example " a, b, c..." then the program will pop up the msg box to warn the user.
    Poping a messagebox will only annoy the user. Instead of then just ensure that nothing is typed in the textbox...

    Try this

    vb Code:
    1. Private Sub textbox_KeyPress(KeyAscii As Integer)
    2.     Dim pos As Long
    3.    
    4.     '~~> Check if the text already has a decimal
    5.     pos = InStr(1, textbox, ".")
    6.    
    7.     If pos > 0 And KeyAscii = 46 Then
    8.         KeyAscii = 0
    9.     ElseIf Not Chr(KeyAscii) Like _
    10.     "[0-9,.]" And KeyAscii <> 8 Then
    11.         KeyAscii = 0
    12.     End If
    13. End Sub
    14.  
    15. Private Sub textbox_LostFocus()
    16.    
    17.     If Val(textbox.Text) < 0.03 Then
    18.         MsgBox "cannot less than 0.03!", vbCritical
    19.         '~~> reset the invalid number
    20.         textbox.Text = ""
    21.     End If
    22. End Sub
    Last edited by Siddharth Rout; Apr 4th, 2009 at 09:06 AM.
    A good exercise for the Heart is to bend down and help another up...
    Please Mark your Thread "Resolved", if the query is solved


    MyGear:
    ★ CPU ★ Ryzen 5 5800X
    ★ GPU ★ NVIDIA GeForce RTX 3080 TI Founder Edition
    ★ RAM ★ G. Skill Trident Z RGB 32GB 3600MHz
    ★ MB ★ ASUS TUF GAMING X570 (WI-FI) ATX Gaming
    ★ Storage ★ SSD SB-ROCKET-1TB + SEAGATE 2TB Barracuda IHD
    ★ Cooling ★ NOCTUA NH-D15 CHROMAX BLACK 140mm + 10 of Noctua NF-F12 PWM
    ★ PSU ★ ANTEC HCG-1000-EXTREME 1000 Watt 80 Plus Gold Fully Modular PSU
    ★ Case ★ LIAN LI PC-O11 DYNAMIC XL ROG (BLACK) (G99.O11DXL-X)
    ★ Monitor ★ LG Ultragear 27" 240Hz Gaming Monitor
    ★ Keyboard ★ TVS Electronics Gold Keyboard
    ★ Mouse ★ Logitech G502 Hero

  3. #3

    Thread Starter
    Lively Member
    Join Date
    Mar 2008
    Posts
    66

    Re: Need help for input textbox

    thanks Koolsid,

    It is such a great code, it works fine in the VB program. but when i try to implement in the Visual Basic Excel, it seems like the LostFocus event cannot take effect, so do i need to change any command on that if i want to implement this code in VBA excel?

    thanks in advance again!

  4. #4
    Discovering Life Siddharth Rout's Avatar
    Join Date
    Feb 2005
    Location
    Mumbai, India
    Posts
    12,001

    Re: Need help for input textbox

    Quote Originally Posted by Johnyc View Post
    thanks Koolsid,

    It is such a great code, it works fine in the VB program. but when i try to implement in the Visual Basic Excel, it seems like the LostFocus event cannot take effect, so do i need to change any command on that if i want to implement this code in VBA excel?

    thanks in advance again!
    For VBA use this

    vb Code:
    1. Private Sub TextBox_Exit(ByVal Cancel As MSForms.ReturnBoolean)
    2.     If Val(TextBox.Text) < 0.03 Then
    3.         MsgBox "cannot less than 0.03!", vbCritical
    4.         '~~> reset the invalid number
    5.         TextBox.Text = ""
    6.     End If
    7. End Sub
    8.  
    9. Private Sub TextBox_KeyPress(ByVal KeyAscii As MSForms.ReturnInteger)
    10.     Dim pos As Long
    11.    
    12.     '~~> Check if the text already has a decimal
    13.     pos = InStr(1, TextBox, ".")
    14.    
    15.     If pos > 0 And KeyAscii = 46 Then
    16.         KeyAscii = 0
    17.     ElseIf Not Chr(KeyAscii) Like _
    18.     "[0-9,.]" And KeyAscii <> 8 Then
    19.         KeyAscii = 0
    20.     End If
    21. End Sub
    A good exercise for the Heart is to bend down and help another up...
    Please Mark your Thread "Resolved", if the query is solved


    MyGear:
    ★ CPU ★ Ryzen 5 5800X
    ★ GPU ★ NVIDIA GeForce RTX 3080 TI Founder Edition
    ★ RAM ★ G. Skill Trident Z RGB 32GB 3600MHz
    ★ MB ★ ASUS TUF GAMING X570 (WI-FI) ATX Gaming
    ★ Storage ★ SSD SB-ROCKET-1TB + SEAGATE 2TB Barracuda IHD
    ★ Cooling ★ NOCTUA NH-D15 CHROMAX BLACK 140mm + 10 of Noctua NF-F12 PWM
    ★ PSU ★ ANTEC HCG-1000-EXTREME 1000 Watt 80 Plus Gold Fully Modular PSU
    ★ Case ★ LIAN LI PC-O11 DYNAMIC XL ROG (BLACK) (G99.O11DXL-X)
    ★ Monitor ★ LG Ultragear 27" 240Hz Gaming Monitor
    ★ Keyboard ★ TVS Electronics Gold Keyboard
    ★ Mouse ★ Logitech G502 Hero

  5. #5
    Former Admin/Moderator MartinLiss's Avatar
    Join Date
    Sep 1999
    Location
    San Jose, CA
    Posts
    33,431

    Re: Need help for input textbox

    You can use my NumberBox.ocx (link in my signature). It will restrict input to numerics and it also has a MinValue property you could set to .3 and no further validation would be required.

  6. #6

    Thread Starter
    Lively Member
    Join Date
    Mar 2008
    Posts
    66

    Re: Need help for input textbox

    Thanks for all the suggestions and solutions. I really appreciate that!

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