Results 1 to 11 of 11

Thread: [RESOLVED] make textbox accept string

  1. #1

    Thread Starter
    Frenzied Member
    Join Date
    Feb 2007
    Location
    Malaysia
    Posts
    1,370

    Resolved [RESOLVED] make textbox accept string

    I would like to allow only string press from keyboad and spacebar are acceptable. How to do that?

  2. #2

  3. #3
    Frenzied Member
    Join Date
    Jul 2007
    Posts
    1,306

    Re: make textbox accept string

    Code:
    Private Sub Text1_KeyPress(KeyAscii As Integer)
    
        If InStr(1, "ABCDEFGHIJKLMNOPQRSTUVWXYZ 1234567890", UCase(Chr(KeyAscii)), vbTextCompare) > 0 Then
        Else
            KeyAscii = 0
        End If
    
    End Sub


    EDIT : remove or add anything you need to allow/disallow in the string
    IIF(Post.Rate > 0 , , )

  4. #4
    PowerPoster RhinoBull's Avatar
    Join Date
    Mar 2004
    Location
    New Amsterdam
    Posts
    24,132

    Re: make textbox accept string

    Using InStr will also prevent you from using say BackSpace which is most likely needed for editing.
    Here is another variation of what I posted previously:
    Code:
    Private Sub Text1_KeyPress(KeyAscii As Integer)
        If Not (KeyAscii = 32 Or KeyAscii = 8) Then 'allow using spacebar and backspace keys
            If Asc(LCase(Chr(KeyAscii))) < 97 Or Asc(LCase(Chr(KeyAscii))) > 122 Then
                'disallow anything but alpha chars (Aa-Zz)
                KeyAscii = 0
            End If
        End If
    End Sub

  5. #5
    PowerPoster
    Join Date
    Jan 2008
    Posts
    11,074

    Re: make textbox accept string

    zeezee,

    Can you please explain how that works?

    It looks like you are taking the keyboard character entered, temporarily making it upper case and then comparing it to the string "ABCDE........ 1234567890" and if found do nothing but if not found then set KeyAscii to 0 but I don't understand it. Me not too bright today.

  6. #6
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333

    Re: make textbox accept string

    Quote Originally Posted by matrik02
    I would like to allow only string press from keyboad and spacebar are acceptable. How to do that?
    In other words, you do not want to allow numeric input, correct?

  7. #7

    Thread Starter
    Frenzied Member
    Join Date
    Feb 2007
    Location
    Malaysia
    Posts
    1,370

    Re: make textbox accept string

    What the difference with your previous code #2 ?

  8. #8

    Thread Starter
    Frenzied Member
    Join Date
    Feb 2007
    Location
    Malaysia
    Posts
    1,370

    Re: make textbox accept string

    Quote Originally Posted by Hack
    In other words, you do not want to allow numeric input, correct?
    Yes.

  9. #9
    Frenzied Member
    Join Date
    Jul 2007
    Posts
    1,306

    Re: make textbox accept string

    @Rhino
    Yeah You are right, Just felt lazy to test it tharoughly
    Sorry for the mistake

    @jmsrickland
    Yeah you are right, just chcecking whether its in the big string (ABCD ...)
    keyascii is ascii of the keypressed, so need to convert to character -> Chr
    Then if its in the big string, it would return the position, a number > 1. So our true part is inverse of that, so if its not in the string, make the keyascii = 0 to prevent it bieng set in the text box.
    Got it?

    IIF(Post.Rate > 0 , , )

  10. #10

  11. #11

    Thread Starter
    Frenzied Member
    Join Date
    Feb 2007
    Location
    Malaysia
    Posts
    1,370

    Re: make textbox accept string

    Thank you RhinoBull and the other, it work now

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