I have a text box that I only want a user to be able to enter integers. I have it so that if a number with a decmail place is enter a error message comes up. But when i was testing the code, I entered a letter in and i got a runtime error.. I was wondeirng what I would use give an error if a letter what typed in.
Don't allow text to be entered in a text box
Collapse
X
-
-
dear,
I hope this will help
Code:Private Sub Text1_Change() With Text1 '§ check for letters If Not IsNumeric(.Text) And (.Text) <> "" Then MsgBox ("Enter a NUMBER !") Else '§ check for DOT or COMMA If InStr(.Text, ".") Or InStr(.Text, ",") Then MsgBox ("Enter an INTEGER value !") Else '§ enter your code.... ' MsgBox ("This is OK") End If End If End With End Sub
Comment
-
-
dear,
The "If Not IsNumeric(.Text ) ..." checks if the string in text1 is a value.
From the moment you type a character, it gives a textbox with a warning.
Just try it.(see attachment)
Notice: I have also entered 2 commands which delete the last wrong character and sets the cursor to the last position.
br,Comment
-
Code:Private Sub txtBoxName_Change() With txtBoxName '§ check for letters If Not IsNumeric(.Text) And (.Text) <> "" Then MsgBox ("Enter a NUMBER !") '§ clear the last character .Text = Left(.Text, Len(.Text) - 1) '§ set the cursor at the end of the string .SelStart = Len(.Text) Else '§ check for DOT or COMMA If InStr(.Text, ".") Or InStr(.Text, ",") Then MsgBox ("Enter an INTEGER value !") Else '§ enter your code.... ' MsgBox ("This is OK") End If End If End With End Sub
Last edited by Rabbit; Sep 9 '13, 04:59 PM. Reason: Please use code tags when posting code or formatted data.Comment
-
This piece of code does exactly the work. But I would like to have a Windows Sound rather than a message box. Is it possible?Comment
-
'I found it from another source
'Copy the following and paste it in frm module (in declatrations division) of your project where you want the sound to be played
Code:Private Declare Function MessageBeep Lib "user32" _ (ByVal wType As Long) As Long Private Const MB_ICONASTERISK = &H40& Private Const MB_ICONEXCLAMATION = &H30& Private Const MB_ICONHAND = &H10& Private Const MB_ICONINFORMATION = MB_ICONASTERISK Private Const MB_ICONMASK = &HF0& Private Const MB_ICONQUESTION = &H20& Private Const MB_ICONSTOP = MB_ICONHAND 'Copy the following and paste it in the textbox_change() module Private Sub txtBoxName_Change() With txtDate1 '§ check for letters If Not IsNumeric(.Text) And (.Text) <> "" Then Call MessageBeep(MB_ICONINFORMATION) '§ clear the last character .Text = Left(.Text, Len(.Text) - 1) '§ set the cursor at the end of the string .SelStart = Len(.Text) Else '§ check for DOT or COMMA and OTHERS If InStr(.Text, ",") Or InStr(.Text, "$") Or InStr(.Text, "-") Or InStr(.Text, "+") Or InStr(.Text, "\") Or InStr(.Text, "*") Then Call MessageBeep(MB_ICONINFORMATION) .Text = Left(.Text, Len(.Text) - 1) Else '§ enter your code.... ' MsgBox ("This is OK") End If End If End With End Sub
Last edited by Rabbit; Sep 9 '13, 05:00 PM. Reason: Please use code tags when posting code or formatted data.Comment
Comment