|
-
Feb 11th, 2001, 01:24 PM
#1
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
-
Feb 11th, 2001, 01:31 PM
#2
I have tried 'IsNull', 'IsObject' - but no luck Please help me stop characters (letters) being entered into a number specfic text box
-
Feb 11th, 2001, 01:35 PM
#3
Hyperactive Member
In the keypress event of the textbox:
Code:
If KeyAscii > 32 And Not(IsNumeric(Chr(KeyAscii))) Then
KeyAscii = 0
Beep
End if
-
Feb 11th, 2001, 01:38 PM
#4
Great thnaks - I am learning VB - so what does the 32 mean???
-
Feb 11th, 2001, 01:42 PM
#5
Hyperactive Member
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.
Last edited by dsy5; Feb 11th, 2001 at 01:47 PM.
Donald Sy - VB (ab)user
-
Feb 11th, 2001, 01:47 PM
#6
thanks very much - I now understand - Thanks a lot
-
Feb 12th, 2001, 08:02 AM
#7
Member
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
-
Feb 12th, 2001, 08:29 AM
#8
Retired VBF Adm1nistrator
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
Microsoft MVP : Visual Developer - Visual Basic [2004-2005]
-
Feb 12th, 2001, 10:04 AM
#9
Fanatic Member
-
Feb 12th, 2001, 11:57 AM
#10
Retired VBF Adm1nistrator
Yes, note the
"Obviously you'd also have check to make sure the character pressed wasnt a backspace or something."
part 
- jamie
Microsoft MVP : Visual Developer - Visual Basic [2004-2005]
-
Feb 12th, 2001, 01:06 PM
#11
Hyperactive Member
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
-
Feb 13th, 2001, 01:06 AM
#12
Fanatic Member
-
Feb 13th, 2001, 02:56 AM
#13
Retired VBF Adm1nistrator
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
Microsoft MVP : Visual Developer - Visual Basic [2004-2005]
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
|