Allowing ONLY text to be typed in my text box - NO NUMBERS 
Author Message
 Allowing ONLY text to be typed in my text box - NO NUMBERS

Hi,

I just wanted to know how I would go about telling my text box to accept
text and no numbers.
Everytime I type some text in (Life say your name) and I press TAB to shift
the focus to another
text box, for some unknown reason I get a Error 13 - Type Mismatch.

Why won't my error handler recognise and allow the text to be left in there
and give me the error
message when I leave numbers in there.... it's like my error handler does
the opposite at the moment.
Please leave some sample code so I know what you are talking about..

Thank You  !!!!

- Greg



Tue, 25 Feb 2003 22:10:29 GMT  
 Allowing ONLY text to be typed in my text box - NO NUMBERS
Private Sub Text1_KeyPress(KeyAscii As Integer)
    Select Case KeyAscii
        Case VbKeyA to VBKeyZ
        Case vbKeyBack
        Case VbKeyReturn
        Case Else
            MsgBox "Letter only.", vbOKOnly, "Error..."
            KeyAscii = 0
    End Select
End Sub

I think vbKeyA - 32 to VBKeyZ - 32 is lower case.  Or is it add 32?
Someone will know.  I can't remember off the top of my head.

Jennifer



Quote:
> Hi,

> I just wanted to know how I would go about telling my text box to
accept
> text and no numbers.
> Everytime I type some text in (Life say your name) and I press TAB to
shift
> the focus to another
> text box, for some unknown reason I get a Error 13 - Type Mismatch.

> Why won't my error handler recognise and allow the text to be left in
there
> and give me the error
> message when I leave numbers in there.... it's like my error handler
does
> the opposite at the moment.
> Please leave some sample code so I know what you are talking about..

> Thank You  !!!!

> - Greg

Sent via Deja.com http://www.deja.com/
Before you buy.


Tue, 25 Feb 2003 22:36:51 GMT  
 Allowing ONLY text to be typed in my text box - NO NUMBERS

Quote:
> Private Sub Text1_KeyPress(KeyAscii As Integer)
>     Select Case KeyAscii
>         Case VbKeyA to VBKeyZ
>         Case vbKeyBack
>         Case VbKeyReturn
>         Case Else
>             MsgBox "Letter only.", vbOKOnly, "Error..."
>             KeyAscii = 0
>     End Select
> End Sub

> I think vbKeyA - 32 to VBKeyZ - 32 is lower case.  Or is it add 32?
> Someone will know.  I can't remember off the top of my head.

Yes, lower case Ascii codes are 32 less then Upper case.  So, to adapt
Jennifer's code slightly....

Private Sub Text1_KeyPress(KeyAscii As Integer)
     Select Case KeyAscii
         Case 97 To 122, 65 To 90 'for figures only Case 46, 48 To 57
         Case vbKeyBack
         Case VbKeyReturn
         Case Else
             MsgBox "Letter only.", vbOKOnly, "Error..."
             KeyAscii = 0
     End Select
 End Sub

Christina

--
Christina West



Wed, 26 Feb 2003 00:35:08 GMT  
 Allowing ONLY text to be typed in my text box - NO NUMBERS
Greg,

Look in the help files at the Like operator:

Private Sub Text1_KeyPress(KeyAscii As Integer)
    If Not (LCase(Chr(KeyAscii)) Like "[a-z]") _
             And KeyAscii >= vbKeySpace Then
        MsgBox "Only characters"
        KeyAscii = 0
    End If
End Sub

Johan.



Quote:
> Hi,

> I just wanted to know how I would go about telling my text box to accept
> text and no numbers.
> Everytime I type some text in (Life say your name) and I press TAB to
shift
> the focus to another
> text box, for some unknown reason I get a Error 13 - Type Mismatch.

> Why won't my error handler recognise and allow the text to be left in
there
> and give me the error
> message when I leave numbers in there.... it's like my error handler does
> the opposite at the moment.
> Please leave some sample code so I know what you are talking about..

> Thank You  !!!!

> - Greg



Wed, 26 Feb 2003 05:11:35 GMT  
 Allowing ONLY text to be typed in my text box - NO NUMBERS

Quote:
> Yes, lower case Ascii codes are 32 less then Upper case.  So, to adapt
> Jennifer's code slightly....

> Private Sub Text1_KeyPress(KeyAscii As Integer)
>      Select Case KeyAscii
>          Case 97 To 122, 65 To 90 'for figures only Case 46, 48 To 57
>          Case vbKeyBack
>          Case VbKeyReturn
>          Case Else
>              MsgBox "Letter only.", vbOKOnly, "Error..."
>              KeyAscii = 0
>      End Select
>  End Sub

No good -- you can still right-click the text box and paste a
number in from the Clipboard.  :)

Regards, Otser.



Wed, 26 Feb 2003 07:29:39 GMT  
 Allowing ONLY text to be typed in my text box - NO NUMBERS
In your text_Change event, have this code:

dim sCheck as string
dim n as integer
dim sConstruct as string
dim sTemp as string

sCheck="abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"

for n = 1 to Len(Text1.Text)
  sTemp = Mid(Text1.Text, n, 1)
  if instr(1, sCheck, sTemp, vbBinaryCompare) <> 0 then
    sConstruct = sConstruct + sTemp
  end if
next

Alternatively, you can check for things that you want to exclude by changing
sCheck to be all the things that can't be entered, and then changing <>0 to
=0.

HTH

steve



Wed, 26 Feb 2003 18:22:33 GMT  
 Allowing ONLY text to be typed in my text box - NO NUMBERS


Quote:

> > Yes, lower case Ascii codes are 32 less then Upper case.  So, to
adapt
> > Jennifer's code slightly....
> > Private Sub Text1_KeyPress(KeyAscii As Integer)
> >      Select Case KeyAscii
> >          Case 97 To 122, 65 To 90 'for figures only Case 46, 48 To
57
> >          Case vbKeyBack
> >          Case VbKeyReturn
> >          Case Else
> >              MsgBox "Letter only.", vbOKOnly, "Error..."
> >              KeyAscii = 0
> >      End Select
> >  End Sub
> No good -- you can still right-click the text box and paste a
> number in from the Clipboard.  :)

Hmph! - the question was about allowing only text to  be /typed/ in a
text box...;)

Damn - I _always_ forget about the dratted pasting!

It would be nice if the Text box had an Allow Paste property!...:)

Christina

--
Christina West



Wed, 26 Feb 2003 07:53:36 GMT  
 Allowing ONLY text to be typed in my text box - NO NUMBERS
'This may be an short alternitive:
'Just check what key has been pressed, and filter it
'These are the keys you need to allow:
'65 trough 91  : letters
'97 trough 123 : capital letters
'8             : Backspace
'32            : Space

Private Sub FilterText(ByRef Key As Integer)

   If ((Key > 64) And (Key < 91)) _
      Or ((Key > 96) And (Key < 123)) _
      Or (Key = 8) Or (Key = 32) Then Exit Sub

   Key = 0

End Sub

'Example Usage:

     Private Sub Text1_KeyPress(KeyAscii As Integer)
         FilterText KeyAscii
    End Sub



Quote:
> In your text_Change event, have this code:

> dim sCheck as string
> dim n as integer
> dim sConstruct as string
> dim sTemp as string

> sCheck="abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"

> for n = 1 to Len(Text1.Text)
>   sTemp = Mid(Text1.Text, n, 1)
>   if instr(1, sCheck, sTemp, vbBinaryCompare) <> 0 then
>     sConstruct = sConstruct + sTemp
>   end if
> next

> Alternatively, you can check for things that you want to exclude by
changing
> sCheck to be all the things that can't be entered, and then changing <>0
to
> =0.

> HTH

> steve



Wed, 26 Feb 2003 22:08:50 GMT  
 Allowing ONLY text to be typed in my text box - NO NUMBERS
Won't work if someone pastes text directly into the box. Trust me, I've been
through this many times, and short of using the API or a custom control
(which the poster appeared to want to avoid) this is the quickest way.

--
steve
[ never argue with a fool, they'll bring you down to their level and then
beat you with experience]



Thu, 27 Feb 2003 05:31:49 GMT  
 Allowing ONLY text to be typed in my text box - NO NUMBERS
Hey Steve,
 Could you give me more info on this API Call ?
 Because I have the same problem with that copy/paste thing.

Thnx.



Quote:
> Won't work if someone pastes text directly into the box. Trust me, I've
been
> through this many times, and short of using the API or a custom control
> (which the poster appeared to want to avoid) this is the quickest way.

> --
> steve
> [ never argue with a fool, they'll bring you down to their level and then
> beat you with experience]



Fri, 28 Feb 2003 13:03:25 GMT  
 Allowing ONLY text to be typed in my text box - NO NUMBERS


[Snip]

Quote:
> > No good -- you can still right-click the text box and paste a
> > number in from the Clipboard.  :)

> Hmph! - the question was about allowing only text to  be /typed/ in a
> text box...;)

> Damn - I _always_ forget about the dratted pasting!

> It would be nice if the Text box had an Allow Paste property!...:)

Well, you could allways instruct the Key_Down event to ignore the Paste-key
..

Or adapt it so it first checks if the material-to-be-pasted has got any
numerical characters, and only than ignore it.

Regards,
  Rudy Wieser



Fri, 28 Feb 2003 14:50:48 GMT  
 
 [ 11 post ] 

 Relevant Pages 

1. Allowing ONLY text to be typed in my text box - NO NUMBERS

2. Filter A Report From a Pop-Up Box for Text, Numbers, and Yes/No Field Types

3. Am I allowed to copy texts and DLLs?

4. Text box text on new page text box

5. code to type in text in a text box

6. Exporting a 3 decimal number to text file produces trunc 2 decimal text number

7. Text-Control (only numbers allowed)

8. Converting text in text boxes to inline text

9. Rich Text Box: Can I remove text and keep multicolored text

10. VBX which is text box which allows line feeds

11. Allowing only certain characters in a text box in vbscript

12. how to get scrolling text in a text box or like Marquee text

 

 
Powered by phpBB® Forum Software