Results 1 to 5 of 5

Thread: [RESOLVED] Trouble with the Keypress event of a textbox

  1. #1

    Thread Starter
    Fanatic Member The_Grudge's Avatar
    Join Date
    Jan 2005
    Location
    Canada
    Posts
    836

    Resolved [RESOLVED] Trouble with the Keypress event of a textbox

    I have the following code in the Keypress event of a textbox.

    VB Code:
    1. If Val(txtRooms) = 0 Then
    2.         MsgBox "Zero is an invalid entry", vbInformation, "Invalid Entry"
    3.         txtRooms = vbNullString
    4.         txtRooms.SetFocus
    5.         Exit Sub
    6.     Else
    7.         Call cmdSubmitChange_Click
    8.     End If

    It works but it doesn't reset the value in the textbox. For example, if the user enters "0", it fires the msgbox saying it's an invalid entry, and then it pops the zero into the textbox. How do I tell it NOT to display the value just entered?

  2. #2
    PowerPoster Static's Avatar
    Join Date
    Oct 2000
    Location
    Rochester, NY
    Posts
    9,390

    Re: Trouble with the Keypress event of a textbox

    u need to intercept & change the KeyAscii

    VB Code:
    1. Private Sub Text1_KeyPress(KeyAscii As Integer)
    2. If KeyAscii = 48 Then '48 = 0
    3.     MsgBox "Zero is an invalid entry", vbInformation, "Invalid Entry"
    4.     KeyAscii = 0
    5.     txtRooms.SetFocus
    6.     Exit Sub
    7. Else
    8.     Call cmdSubmitChange_Click
    9. End If
    10. End Sub
    JPnyc rocks!! (Just ask him!)
    If u have your answer please go to the thread tools and click "Mark Thread Resolved"

  3. #3
    Super Moderator manavo11's Avatar
    Join Date
    Nov 2002
    Location
    Around the corner from si_the_geek
    Posts
    7,171

    Re: Trouble with the Keypress event of a textbox

    Try this :

    VB Code:
    1. If Val(txtRooms) = 0 Then
    2.         MsgBox "Zero is an invalid entry", vbInformation, "Invalid Entry"
    3.         txtRooms = vbNullString
    4.         txtRooms.SetFocus
    5.         [b]KeyAscii = 0[/b]
    6.         Exit Sub
    7.     Else
    8.         Call cmdSubmitChange_Click
    9.     End If

    Not sure it'll work though...


    Has someone helped you? Then you can Rate their helpful post.

  4. #4

    Thread Starter
    Fanatic Member The_Grudge's Avatar
    Join Date
    Jan 2005
    Location
    Canada
    Posts
    836

    Re: Trouble with the Keypress event of a textbox

    Thanks for the help guys.

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

    Re: Trouble with the Keypress event of a textbox

    Well, then move the reset line above the messagebox line. That'll take care of that sucker right quick!

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