Results 1 to 5 of 5

Thread: Accepting only Letters,Backspace nd space as input for taxtbox

  1. #1

    Thread Starter
    Member
    Join Date
    Feb 2017
    Posts
    42

    Accepting only Letters,Backspace nd space as input for taxtbox

    Hi
    I have a textbox in my form and want to validate data input there.
    Code:
        Private Sub text1_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles text1.KeyPress
                If (Microsoft.VisualBasic.Asc(e.KeyChar) < 65) _
                      Or (Microsoft.VisualBasic.Asc(e.KeyChar) > 90) _
                      And (Microsoft.VisualBasic.Asc(e.KeyChar) < 97) _
                      Or (Microsoft.VisualBasic.Asc(e.KeyChar) > 122) Then
                    'space allowed
                    If (Microsoft.VisualBasic.Asc(e.KeyChar) <> 32) Then
                        e.Handled = True
                    End If
                End If
                If (Microsoft.VisualBasic.Asc(e.KeyChar) = 8) Then
                    e.Handled = False
                End If
            End Sub
    My textbox name is txtName ,I replaced Text1 with txtName but got error. (user-defined type not defined) which parameter should i change till i get answer
    Thanx

    I,m absolutely blank about the difference bet vb.NEt, vb6 ....
    Last edited by mortezataheri; Mar 12th, 2017 at 08:19 AM.

  2. #2
    PowerPoster
    Join Date
    Sep 2006
    Location
    Egypt
    Posts
    2,579

    Re: Accepting only Letters,Backspace nd space as input for taxtbox

    What exactly you asking for? the code you posted is VB.NET but you ask in Office forum! Do you want to convert it to VB6 equivalent?



  3. #3

    Thread Starter
    Member
    Join Date
    Feb 2017
    Posts
    42

    Re: Accepting only Letters,Backspace nd space as input for taxtbox

    1-I have a userform in Excel 2007 workbook and want a code like that to validate input data.

    2-
    What exactly you asking for? the code you posted is VB.NET but you ask in Office forum! Do you want to convert it to VB6 equivalent?
    I used to ask Questions like this in VB6 forum till at last the Holy masters of VB6 got tired and bored, so after a long long session came to the point to kick my a** and throw me here they said : "aaa... this thread is not a VB class question so better to send it to Office Development Forum" .that.s why i wander around here all the time.

    P.S. I just wanted to change the atmosphere

  4. #4
    PowerPoster
    Join Date
    Sep 2006
    Location
    Egypt
    Posts
    2,579

    Re: Accepting only Letters,Backspace nd space as input for taxtbox

    This is what you should use for Excel 2007
    Code:
    Private Sub TextBox1_KeyPress(ByVal KeyAscii As MSForms.ReturnInteger)
        Select Case KeyAscii
            Case 65 To 90, 97 To 122, 32, 8
                 ' Allow A-Z, a-z, Space, Back
            Case Else
                KeyAscii = 0 ' Suppress others
            End Select
    End Sub



  5. #5
    Don't Panic! Ecniv's Avatar
    Join Date
    Nov 2000
    Location
    Amsterdam...
    Posts
    5,343

    Re: Accepting only Letters,Backspace nd space as input for taxtbox

    Or just validate it before saving... throw an error back to the user about data type/entry being incorrect...?

    BOFH Now, BOFH Past, Information on duplicates

    Feeling like a fly on the inside of a closed window (Thunk!)
    If I post a lot, it is because I am bored at work! ;D Or stuck...
    * Anything I post can be only my opinion. Advice etc is up to you to persue...

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