Results 1 to 13 of 13

Thread: Stop Letters being entered into a Text Box

  1. #1
    rlculver
    Guest
    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

  2. #2
    Guest
    I have tried 'IsNull', 'IsObject' - but no luck Please help me stop characters (letters) being entered into a number specfic text box

  3. #3
    Hyperactive Member marnitzg's Avatar
    Join Date
    Oct 2000
    Location
    South Africa
    Posts
    372
    In the keypress event of the textbox:
    Code:
    If KeyAscii > 32 And Not(IsNumeric(Chr(KeyAscii))) Then
          KeyAscii = 0
          Beep
    End if

  4. #4
    Guest
    Great thnaks - I am learning VB - so what does the 32 mean???

  5. #5
    Hyperactive Member dsy5's Avatar
    Join Date
    Jul 2000
    Location
    Lockport, NY
    Posts
    362
    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

  6. #6
    Guest
    thanks very much - I now understand - Thanks a lot

  7. #7
    Member
    Join Date
    Feb 2001
    Location
    Pakistan
    Posts
    49

    Thumbs down 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

  8. #8
    Retired VBF Adm1nistrator plenderj's Avatar
    Join Date
    Jan 2001
    Location
    Dublin, Ireland
    Posts
    10,359
    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]

  9. #9
    Fanatic Member faisalkm's Avatar
    Join Date
    Oct 2000
    Location
    Germany
    Posts
    752
    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
    Faisal Muhammed
    Homepage:I Started making it in 1994 ...Still Under Construction
    Using

    Visual Basic 6.0 Enterprise SP5
    Embedded Visual Basic 3.0
    SQL Server 2000
    Windows 2000 Proff
    Delphi 6.0


    Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning.

  10. #10
    Retired VBF Adm1nistrator plenderj's Avatar
    Join Date
    Jan 2001
    Location
    Dublin, Ireland
    Posts
    10,359
    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]

  11. #11
    Hyperactive Member marnitzg's Avatar
    Join Date
    Oct 2000
    Location
    South Africa
    Posts
    372
    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

  12. #12
    Fanatic Member faisalkm's Avatar
    Join Date
    Oct 2000
    Location
    Germany
    Posts
    752
    I'm sorry ...I did'nt noticed that...my mistake........
    Faisal Muhammed
    Homepage:I Started making it in 1994 ...Still Under Construction
    Using

    Visual Basic 6.0 Enterprise SP5
    Embedded Visual Basic 3.0
    SQL Server 2000
    Windows 2000 Proff
    Delphi 6.0


    Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning.

  13. #13
    Retired VBF Adm1nistrator plenderj's Avatar
    Join Date
    Jan 2001
    Location
    Dublin, Ireland
    Posts
    10,359
    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
  •  



Click Here to Expand Forum to Full Width