-
Hi All,
How do yuo stop Letters being entered into a text box. I know the variable used to stip Numbers- but how about Characters?????
I used this for Numbers : -
Code:
Dim text1 As Integer
Dim text2 As Integer
Dim text3 As Integer
text1 = InStr(1, txtName.Text, "")
text2 = InStr(1, txtAge.Text, "")
text3 = InStr(1, txtemail.Text, "@")
If text1 = 0 Then
MsgBox "Please Enter Your Name"
Else
If text2 = 0 Then
MsgBox "Please Enter your Age"
Else
If text3 = 0 Then
MsgBox "Please Enter a Valid Email Address"
End If
End If
End If
If txtName.Text <> vbNullString And txtAge.Text <> vbNullString And txtemail.Text <> vbNullString Then
Call Check_details
Unload frmUser
End If
End Sub
Thanks
-
I have tried 'IsNull', 'IsObject' - but no luck Please help me stop characters (letters) being entered into a number specfic text box :(
-
In the keypress event of the textbox:
Code:
If KeyAscii > 32 And Not(IsNumeric(Chr(KeyAscii))) Then
KeyAscii = 0
Beep
End if
-
Great thnaks - I am learning VB - so what does the 32 mean???
-
Well if you know how to stop numbers, you know how to stop letters!
Code:
'stop numbers
Private Sub Text1_KeyPress(KeyAscii As Integer)
If (IsNumeric(Chr(KeyAscii))) Then KeyAscii = 0
End Sub
Code:
'stop letters
Private Sub Text1_KeyPress(KeyAscii As Integer)
If Not (IsNumeric(Chr(KeyAscii))) Then KeyAscii = 0
End Sub
This does't remove all the other possible characters, such as puntuation and control characters.
But you can check for the values between 0 and 58 and then over 122 and simply eliminate them.
Code:
Private Sub Text1_KeyPress(KeyAscii As Integer)
If KeyAscii > 0 or KeyAscii < 58 Then
KeyAscii = 0
Else
If KeyAscii > 122 Then KeyAscii = 0
EndIf
End Sub
This will give you only letters.
The numbers are the decimal Ascii number corresponding to the character. 32 is a space.
See your VB help, there is an Ascii chart in it.
-
thanks very much - I now understand - Thanks a lot :)
-
only alphabets
Hello
the code still has a subtle error. The events you handled won't tackle the case, if the user just copies text from some other control and pastes in the active control.
Regards
-
Well, it hasnt been said yet, so I suppose I'll have to do it. You could always just check if the character entered is in a list of ok characters ;
Code:
Option Explicit
Private Const OK_CHARS As String = "0123456789"
Private Sub Text1_KeyPress(KeyAscii As Integer)
If (InStr(1, OK_CHARS, Chr(KeyAscii), vbTextCompare) = 0) Then KeyAscii = 0
End Sub
Obviously you'd also have check to make sure the character pressed wasnt a backspace or something.
- jamie
-
The above code will not allow the user to press the backspace, and delete keys....u should also include that to provide editing for the user
-
Yes, note the
"Obviously you'd also have check to make sure the character pressed wasnt a backspace or something."
part ;)
- jamie
-
Just a note to some on this thread. Instead of :
Code:
If X > 10 And X < 11 Then
'do something
Else
If X > 1 And X < 2 Then
'do something
End If
End If
Use the Elseif statement. You then only need 1 End if :
Code:
If X > 10 And X < 11 Then
'do something
ElseIf X > 1 And X < 2 Then
'do something
End If
-
I'm sorry ...I did'nt noticed that...my mistake........
-
Just in relation to what marnitzg said,
I've found that using Select Case statements is faster than using if statements.
If you try the following code :
Code:
Option Explicit
Private Declare Function GetTickCount Lib "kernel32" () As Long
Private Sub Form_Load()
Dim x As Long
Dim i As Long
Dim currenttick As Long
x = 11
currenttick = GetTickCount()
For i = 0 To 10000000
Select Case x
Case Is > 10 And x < 20
'blah
Case Is > 1 And x < 2:
'blah
End Select
Next i
MsgBox "Using Select Statements : " & GetTickCount - currenttick
currenttick = GetTickCount()
For i = 0 To 10000000
If x > 10 And x < 11 Then
'do something
ElseIf x > 1 And x < 2 Then
'do something
End If
Next i
MsgBox "Using If Statements : " & GetTickCount - currenttick
End Sub
On my system ;
Select Case Statements : 3410
If Statements : 3860
- jamie