Results 1 to 10 of 10

Thread: [RESOLVED] Limiting textbox characters

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Sep 2009
    Posts
    124

    Resolved [RESOLVED] Limiting textbox characters

    Hi I have a program where i want the texbox only to input numbers i have the ascii coding but the problem is that it is a private sub so even when i dont have to put in data into the textbox it still does the ascii check. here is my coding

    Private Sub txtfirstname_KeyPress(KeyAscii As Integer)
    Select Case KeyAscii
    Case 33 To 64, 91 To 96, 123 To 126
    MsgBox ("Only letters supported for this field! Please input a name.")
    KeyAscii = 0
    Exit Sub
    End Select

    End Sub

  2. #2
    Discovering Life Siddharth Rout's Avatar
    Join Date
    Feb 2005
    Location
    Mumbai, India
    Posts
    12,001

    Re: Limiting textbox characters

    See post 13 in

    http://www.vbforums.com/showthread.p...85#post3739085

    and amend accordingly...
    A good exercise for the Heart is to bend down and help another up...
    Please Mark your Thread "Resolved", if the query is solved


    MyGear:
    ★ CPU ★ Ryzen 5 5800X
    ★ GPU ★ NVIDIA GeForce RTX 3080 TI Founder Edition
    ★ RAM ★ G. Skill Trident Z RGB 32GB 3600MHz
    ★ MB ★ ASUS TUF GAMING X570 (WI-FI) ATX Gaming
    ★ Storage ★ SSD SB-ROCKET-1TB + SEAGATE 2TB Barracuda IHD
    ★ Cooling ★ NOCTUA NH-D15 CHROMAX BLACK 140mm + 10 of Noctua NF-F12 PWM
    ★ PSU ★ ANTEC HCG-1000-EXTREME 1000 Watt 80 Plus Gold Fully Modular PSU
    ★ Case ★ LIAN LI PC-O11 DYNAMIC XL ROG (BLACK) (G99.O11DXL-X)
    ★ Monitor ★ LG Ultragear 27" 240Hz Gaming Monitor
    ★ Keyboard ★ TVS Electronics Gold Keyboard
    ★ Mouse ★ Logitech G502 Hero

  3. #3
    Addicted Member Optional's Avatar
    Join Date
    Jan 2010
    Location
    Rudimentary Space
    Posts
    214

    Re: Limiting textbox characters

    Quote Originally Posted by TechKid View Post
    Hi I have a program where i want the texbox only to input numbers i have the ascii coding but the problem is that it is a private sub so even when i dont have to put in data into the textbox it still does the ascii check. here is my coding
    I don't understand that part of the post. the KeyPress event is only fired when you press a key. So if you have no data to enter you don't press a key. Why would the event trigger ? As a side note, Tabbing through the control will not trigger the event as the TAB key is "special" that way.

    Can you clarify what you mean by "when I dont have to put in data into the textbox it still does the ascii check" ?



    Kind Regards,
    Optional



    If you feel this post has helped in answering your question please return the favour and Rate this post.
    If your problem has been solved and your question has been answered mark the thread as [RESOLVED] by selecting the Thread Tools menu option at the top and clicking the Mark Thread Resolved menu item.


    VB6 - (DataGrid) Get the Row selected with the right mouse button



  4. #4

    Thread Starter
    Lively Member
    Join Date
    Sep 2009
    Posts
    124

    Re: Limiting textbox characters

    Quote Originally Posted by koolsid View Post
    See post 13 in

    http://www.vbforums.com/showthread.p...85#post3739085

    and amend accordingly...
    I already have the coding and it works. My problem is i only want it to be enabled when I click add and the texbox is able to accept input. At the begining of my program the textbox is not allowed to acced input until i click add. When i start my program even before i click add the texbox is already formatted and that is my problem

  5. #5

    Thread Starter
    Lively Member
    Join Date
    Sep 2009
    Posts
    124

    Re: Limiting textbox characters

    Quote Originally Posted by Optional View Post
    I don't understand that part of the post. the KeyPress event is only fired when you press a key. So if you have no data to enter you don't press a key. Why would the event trigger ? As a side note, Tabbing through the control will not trigger the event as the TAB key is "special" that way.

    Can you clarify what you mean by "when I dont have to put in data into the textbox it still does the ascii check" ?
    Ok The boxes are used for displaying and inputting information. When it is only diplaying it still checks if the textbox if it has only letters and if i type a number it gives an error. I want it to only check for letters when i click add. I think the problem is because I have it under the private sub. I need to get it under the Add procedure so that it can only check when i click add

  6. #6
    Addicted Member Optional's Avatar
    Join Date
    Jan 2010
    Location
    Rudimentary Space
    Posts
    214

    Re: Limiting textbox characters

    I'm assuming when you only display data your textbox.enabled property is set to false ?

    Would a validation of the enabled property help ?
    VB Code:
    1. Private Sub txtfirstname_KeyPress(KeyAscii As Integer)
    2.     If (Not (txtfirstname.Enabled)) Then Exit Sub
    3.  
    4.     Select Case KeyAscii
    5.     Case 33 To 64, 91 To 96, 123 To 126
    6.         MsgBox ("Only letters supported for this field! Please input a name.")
    7.         KeyAscii = 0
    8.         Exit Sub
    9.     End Select
    10. End Sub



    Kind Regards,
    Optional



    If you feel this post has helped in answering your question please return the favour and Rate this post.
    If your problem has been solved and your question has been answered mark the thread as [RESOLVED] by selecting the Thread Tools menu option at the top and clicking the Mark Thread Resolved menu item.


    VB6 - (DataGrid) Get the Row selected with the right mouse button



  7. #7
    Discovering Life Siddharth Rout's Avatar
    Join Date
    Feb 2005
    Location
    Mumbai, India
    Posts
    12,001

    Re: Limiting textbox characters

    Ok... Remove that code from Keypress and validate the contents of the textbox in the Add event...

    Something like

    Code:
    Private Sub CmdAdd_Click()
        For i = 1 To Len(Text1.Text)
            If Asc(Mid(Text1.Text, i, 1)) > 47 And _
            Asc(Mid(Text1.Text, i, 1)) < 58 Then
                MsgBox "Textbox cannot have numbers"
                Exit For
            End If
        Next i
    End Sub
    Last edited by Siddharth Rout; Mar 9th, 2010 at 11:48 AM.
    A good exercise for the Heart is to bend down and help another up...
    Please Mark your Thread "Resolved", if the query is solved


    MyGear:
    ★ CPU ★ Ryzen 5 5800X
    ★ GPU ★ NVIDIA GeForce RTX 3080 TI Founder Edition
    ★ RAM ★ G. Skill Trident Z RGB 32GB 3600MHz
    ★ MB ★ ASUS TUF GAMING X570 (WI-FI) ATX Gaming
    ★ Storage ★ SSD SB-ROCKET-1TB + SEAGATE 2TB Barracuda IHD
    ★ Cooling ★ NOCTUA NH-D15 CHROMAX BLACK 140mm + 10 of Noctua NF-F12 PWM
    ★ PSU ★ ANTEC HCG-1000-EXTREME 1000 Watt 80 Plus Gold Fully Modular PSU
    ★ Case ★ LIAN LI PC-O11 DYNAMIC XL ROG (BLACK) (G99.O11DXL-X)
    ★ Monitor ★ LG Ultragear 27" 240Hz Gaming Monitor
    ★ Keyboard ★ TVS Electronics Gold Keyboard
    ★ Mouse ★ Logitech G502 Hero

  8. #8

    Thread Starter
    Lively Member
    Join Date
    Sep 2009
    Posts
    124

    Re: Limiting textbox characters

    Thanks guys It worked. thank you so much

  9. #9
    Addicted Member Optional's Avatar
    Join Date
    Jan 2010
    Location
    Rudimentary Space
    Posts
    214

    Re: Limiting textbox characters

    Edit: - deleted post - Didn't see koolsid already posted the solution.



    Kind Regards,
    Optional



    If you feel this post has helped in answering your question please return the favour and Rate this post.
    If your problem has been solved and your question has been answered mark the thread as [RESOLVED] by selecting the Thread Tools menu option at the top and clicking the Mark Thread Resolved menu item.


    VB6 - (DataGrid) Get the Row selected with the right mouse button



  10. #10

    Thread Starter
    Lively Member
    Join Date
    Sep 2009
    Posts
    124

    Re: [RESOLVED] Limiting textbox characters

    Im going to use both of them

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