|
-
Apr 4th, 2009, 08:11 AM
#1
Thread Starter
Lively Member
[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.
-
Apr 4th, 2009, 08:56 AM
#2
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:
Private Sub textbox_KeyPress(KeyAscii As Integer) Dim pos As Long '~~> Check if the text already has a decimal pos = InStr(1, textbox, ".") If pos > 0 And KeyAscii = 46 Then KeyAscii = 0 ElseIf Not Chr(KeyAscii) Like _ "[0-9,.]" And KeyAscii <> 8 Then KeyAscii = 0 End If End Sub Private Sub textbox_LostFocus() If Val(textbox.Text) < 0.03 Then MsgBox "cannot less than 0.03!", vbCritical '~~> reset the invalid number textbox.Text = "" End If 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
-
Apr 4th, 2009, 12:14 PM
#3
Thread Starter
Lively Member
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!
-
Apr 4th, 2009, 12:57 PM
#4
Re: Need help for input textbox
 Originally Posted by Johnyc
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:
Private Sub TextBox_Exit(ByVal Cancel As MSForms.ReturnBoolean) If Val(TextBox.Text) < 0.03 Then MsgBox "cannot less than 0.03!", vbCritical '~~> reset the invalid number TextBox.Text = "" End If End Sub Private Sub TextBox_KeyPress(ByVal KeyAscii As MSForms.ReturnInteger) Dim pos As Long '~~> Check if the text already has a decimal pos = InStr(1, TextBox, ".") If pos > 0 And KeyAscii = 46 Then KeyAscii = 0 ElseIf Not Chr(KeyAscii) Like _ "[0-9,.]" And KeyAscii <> 8 Then KeyAscii = 0 End If 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
-
Apr 4th, 2009, 05:01 PM
#5
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.
-
Apr 4th, 2009, 09:37 PM
#6
Thread Starter
Lively Member
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|