[RESOLVED] disallowing text input in a textbox
ok so the first one was a success, but now i want to disallow the user from typing anything but numbers into the textbox, since you cannot multiply, add, divide or subtract a letter, or any other symbol. if there is no way to actually stop the user from typing these, how can i make an elseif statement to only allow the calculator to allow a number between 1 and 13034431, and nothing else, and then display
Code:
msgbox "Please type a proper number"
:)
Re: disallowing text input in a textbox
Something like this...????
Code:
Private Sub Text1_KeyPress(KeyAscii As Integer)
If KeyAscii >= 48 And KeyAscii <= 57 Then
'
Else
KeyAscii = 0
End If
End Sub
Re: disallowing text input in a textbox
There are a few ways of doing this.
Easiest would be to add this in your _LostFocus() Event
Code:
If IsNumeric(text1.text) = False then
msgbox "Please type a proper number"
text1.setfocus
End If
Re: disallowing text input in a textbox
Or something like this..????:)
Code:
Private Sub Text1_Change()
If IsNumeric(Text1.Text) = False Then
MsgBox "Only numbers are allowed..!!"
Text1.SetFocus
Text1.SelStart = 0
Text1.SelLength = Len(Text1.Text)
Exit Sub
End If
If Val(Text1.Text) >= 1 And Val(Text1.Text) <= 13034431 Then
'
Else
Text1.Text = "1"
Text1.SetFocus
Text1.SelStart = 0
Text1.SelLength = Len(Text1.Text)
MsgBox "The limit is from 1 to 13034431", vbCritical, "Exceeds limit...!"
End If
End Sub
Re: disallowing text input in a textbox
akhileshbc, both were good, but disallowed the use of backspace for the first set of code, and the message came up when backspace was pressed for the second one.
some1uk03, worked, but the error still came up after the message arrived.
any solution to allow the backspace as well?
sorry am n00b :p
Re: disallowing text input in a textbox
Try this...:)
Code:
Private Sub Text1_Change()
If Trim(Text1.Text) = "" Then Exit Sub '''''''''''''''''''''''''''''''''''''''''newly introduced
If IsNumeric(Text1.Text) = False Then
MsgBox "Only numbers are allowed..!!"
Text1.SetFocus
Text1.SelStart = 0
Text1.SelLength = Len(Text1.Text)
Exit Sub
End If
If Val(Text1.Text) >= 1 And Val(Text1.Text) <= 13034431 Then
'
Else
Text1.Text = "1"
Text1.SetFocus
Text1.SelStart = 0
Text1.SelLength = Len(Text1.Text)
MsgBox "The limit is from 1 to 13034431", vbCritical, "Exceeds limit...!"
End If
End Sub
Re: disallowing text input in a textbox
is there anything you can't do?? :p
legend mate, i'll keep you yet. ^^
Re: [RESOLVED] disallowing text input in a textbox
Try this (backspace is 8)
Code:
Private Sub Text1_KeyPress(KeyAscii As Integer)
If (KeyAscii < 48 Or KeyAscii > 57) And KeyAscii <> 46 And KeyAscii <> 13 _
And KeyAscii <> 8 Then
MsgBox "Only numbers are allowed for this entries"
KeyAscii = 0
Exit Sub
End If
End Sub
'also, in the change event put this code
'to prevent pasting in non numeric text
Private Sub Text1_Change()
If Not IsNumeric(Text1.Text) Then
MsgBox "Only Numbers Are Allowed"
Text1.Text = vbNullString
Exit Sub
End If