Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Text Filter For TextBox

Status
Not open for further replies.

1SorryDog

Technical User
Jul 15, 2002
25
0
0
US
Is there a way to filter text entered into a textbox so as to only allow alphabetic chataters to be typed ?? No special charaters ( !"'./,$% ) allowed as this data is appended to the end of an SQL statement.

Any help would be appreciated.


Private Sub txtComment_KeyPress(KeyAscii As Integer)
If KeyAscii = Asc(vbCr) Then
KeyAscii = 0
End If

'need text filter

End Sub

Thanks...
 
In the KeyUp Event, check the KeyAscii code and ensure that it is within the alpha range you want. If it is just exit the sub else set Cancel=true
 
KeyASCII values for A-Z are 97 - 122 test for these and cancel if out of range Gary Parker
Systems Support Analyst
 
Sorry I'll make that more detailed (and accurate)! In the KeyPress event do:

If KeyAscii = 72 Then
KeyAscii = vbNull
End If

This example allows entry of any characters EXCEPT uppercase H. So you will need to alter the IF statement to fulfil yur requirements
 
my users are very clever and if they can't type it in, they paste it and if they can't paste by keys they paste by mouse... Be aware that it really is possible to enter not allowed characters if you only check in the keypress or keyup events. And murphy says, if it is possible, they will enter trash!
Another approach could be:
Code:
Private Sub Text1_Change()
    Static strLastText As string  ' holds last valid text
    Dim lngPos As Long
    Dim lngMax As Long
    Dim strText As String
    Dim objTextbox As TextBox

    Set objTextbox = Me.ActiveControl
    strText = objTextbox.Text
    lngMax = Len(strText)
    For lngPos = 1 To lngMax
        If UCase(Mid(strText, lngPos, 1)) = LCase(Mid(strText, lngPos, 1)) Then
            objTextbox.Text = strLastText
            objTextbox.SelStart = lngPos - 1
            Exit For
        End If
    Next lngPos
    strLastText = objTextbox.Text
    Set objTextbox = Nothing
End Sub
Take this sample just as suggestion, it's not tested at all!

hope this helps
Andreas

 

Give this a try... its simple to add number or spaces if you want "0" to "9" or " "

[tt]
Option Explicit

Private Sub Text1_KeyPress(KeyAscii As Integer)

On Error GoTo Text1_KeyPressError

Dim Char As String

If KeyAscii = vbKeyBack Then Exit Sub
If KeyAscii = vbKeyTab Then Exit Sub

Char = Chr(KeyAscii)
Select Case Char
Case "A" To "Z"
Case "a" To "z"
Case Else
KeyAscii = 0
End Select

Exit Sub
Text1_KeyPressError:

MsgBox Err.Description

End Sub

[/tt]
 
Thanks for all of your suggestion's folks!!!!

I ended up using another vairation of code...

This does exactly what I want...

Private Sub txtComment_Change()
Dim char As String

char = Right(txtComment.Text, 1)

Select Case char
Case "A" To "Z"
Case "a" To "z"
Case "0" To "9"
Case Chr(32)
Case Chr(8)

Case Else
SendKeys "{BS}" 'Invalid charater, send backspace to textbox.
End Select

End Sub
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top