Results 1 to 2 of 2

Thread: Q: Restricting Characters in a text box

  1. #1

    Thread Starter
    New Member
    Join Date
    Oct 2000
    Posts
    12
    How Do I go about setting up restrictions on what can be typed into a text box? I need it set up so that the spacebar can't be used, as well as ascii symbols. (Basically I only want my users to be able to use letters and numbers)

    Thanks
    Adrift

  2. #2
    PowerPoster BruceG's Avatar
    Join Date
    May 2000
    Location
    New Jersey (USA)
    Posts
    2,657
    You can restrict characters by using the KeyPress event:
    Code:
    Private Sub Text1_KeyPress(KeyAscii As Integer)
    
        Dim strUserChar  As String
    
        ' Allow "control characters" like backspace and
        ' and arrow keys ...
        If KeyAscii < 32 Then Exit Sub
    
        ' If the character is anything other than a letter
        ' or number, set the KeyAscii argument to 0, which
        ' converts the user's keypress to "null" ...
    
        strUserChar = Chr$(KeyAscii)
    
        If (strUserChar >= "0" And strUserChar <= "9") _
        Or (strUserChar >= "A" And strUserChar <= "Z") _
        Or (strUserChar >= "a" And strUserChar <= "z") Then
            ' it's OK, do nothing
        Else
            KeyAscii = 0
        End If
    
    End Sub
    "It's cold gin time again ..."

    Check out my website here.

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