[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.
Re: Need help for input textbox
Quote:
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
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!
Re: Need help for input textbox
Quote:
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
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.
Re: Need help for input textbox
Thanks for all the suggestions and solutions. I really appreciate that!