force only letter in textbox

User avatar
sal21
PlatinumLounger
Posts: 4497
Joined: 26 Apr 2010, 17:36

force only letter in textbox

Post by sal21 »

I use this to set only in ucase the text.
But i need also to set only a letter in textbox.

Code: Select all

Private Sub TNOMINATIVO_KeyPress(KeyAscii As Integer)

    KeyAscii = Asc(UCase(Chr(KeyAscii)))

End Sub

User avatar
SpeakEasy
5StarLounger
Posts: 616
Joined: 27 Jun 2021, 10:46

Re: force only letter in textbox

Post by SpeakEasy »

Code: Select all

Private Sub Text1_KeyPress(KeyAscii As Integer)
    Select Case KeyAscii
        Case 65 To 90, 97 To 122
            KeyAscii = Asc(UCase$(Chr$(KeyAscii)))
        Case Else
            KeyAscii = 0
    End Select
End Sub

User avatar
sal21
PlatinumLounger
Posts: 4497
Joined: 26 Apr 2010, 17:36

Re: force only letter in textbox

Post by sal21 »

SpeakEasy wrote:
04 Apr 2022, 18:30

Code: Select all

Private Sub Text1_KeyPress(KeyAscii As Integer)
    Select Case KeyAscii
        Case 65 To 90, 97 To 122
            KeyAscii = Asc(UCase$(Chr$(KeyAscii)))
        Case Else
            KeyAscii = 0
    End Select
End Sub
TKS,
but this code dont permit to delete a single char and dont permit to use the tab for deletation....

User avatar
SpeakEasy
5StarLounger
Posts: 616
Joined: 27 Jun 2021, 10:46

Re: force only letter in textbox

Post by SpeakEasy »

>this code dont permit to delete a single char
Well, that depends - Del works fine, as that is not interpreted as an ASCII controlcode. Backspace, however, will; not work because you asked for letters only, and backspace is not a letter

>and dont permit to use the tab for deletation....
Tab to delete? Huh? In what world. Tab is used to move between tabable controls on a form. Not to delete. And no, it won't work with this code beccasue, again, it isn't a letter, it is a control code.

To restore backspace and Tab functionality to their defaults iwe simply need to modifiy our code as follows:

Code: Select all

Private Sub Text1_KeyPress(KeyAscii As Integer)
    Select Case KeyAscii
        Case 65 To 90, 97 To 122, 8, 9
            KeyAscii = Asc(UCase$(Chr$(KeyAscii)))
        Case Else
            KeyAscii = 0
    End Select
End Sub

User avatar
SpeakEasy
5StarLounger
Posts: 616
Joined: 27 Jun 2021, 10:46

Re: force only letter in textbox

Post by SpeakEasy »

By the way <space> is also not a letter (and thus not currently handled in the code above). If you need to add support for <space> you should now have sufficient info from the examples on how to do that yourself.

CData
3StarLounger
Posts: 308
Joined: 24 Dec 2015, 16:41

Re: force only letter in textbox

Post by CData »

A late post, and perhaps not relevant: if this is an Access form - it can be handled with a form mask property and/or table field property

User avatar
HansV
Administrator
Posts: 79444
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: force only letter in textbox

Post by HansV »

@CData: it's about VB6.
Best wishes,
Hans