Results 1 to 19 of 19

Thread: Prevent characters from being typed

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Nov 2003
    Location
    Connecticut
    Posts
    257

    Prevent characters from being typed

    I am trying to program a text box to only accept numbers. I assume you would program the textchanged event of the text box to ignore the keystroke if it is something other than a number? Is that correct or is there a better way to do it?

  2. #2
    Hyperactive Member spoiledkid's Avatar
    Join Date
    Oct 2002
    Location
    Canada
    Posts
    437
    VB Code:
    1. Option Explicit
    2.  
    3. Private Sub Text1_KeyPress(KeyAscii As Integer)
    4.     If KeyAscii = vbKeyBack Then Exit Sub
    5.     If KeyAscii >= Asc("0") And _
    6.         KeyAscii <= Asc("9") Then Exit Sub
    7.        
    8.     KeyAscii = 0
    9. End Sub

  3. #3
    Hyperactive Member maxl's Avatar
    Join Date
    Jan 2002
    Location
    Montréal
    Posts
    384
    You have to be careful with the pasting too. Keypress event doesn't trap the user pasting text in the textbox.
    COBOL sa suce !!!

  4. #4

    Thread Starter
    Hyperactive Member
    Join Date
    Nov 2003
    Location
    Connecticut
    Posts
    257
    Thanks,

    I tried that and it's not working. Is the vbKeyBack either a function I'm not familiar with or needs to be declared somewhere? I apoligize ahead of time if that's a stupid question. I'm relatively new at this.

  5. #5
    Former Admin/Moderator MartinLiss's Avatar
    Join Date
    Sep 1999
    Location
    San Jose, CA
    Posts
    33,431
    You can also click the NumberBox ActiveX control link in my signature. That will get you to code that you can compile to produce an OCX that limits input to numbers. It also allows you to specify maximim decimals and to decide if negative numbers are allowed.

  6. #6

    Thread Starter
    Hyperactive Member
    Join Date
    Nov 2003
    Location
    Connecticut
    Posts
    257
    Thanks Marty,

    Since I am new to programming, how can I compile this code and add the control to my toolbox or project?

  7. #7
    Former Admin/Moderator MartinLiss's Avatar
    Join Date
    Sep 1999
    Location
    San Jose, CA
    Posts
    33,431
    Just compile it like any other program, save the resulting OCX anyplace you want to (it's probably best to put it where your other OCXs are), go to Project|Components, browse for the new OCX, select it and then just add it to your form like the normal checkbox.

  8. #8

    Thread Starter
    Hyperactive Member
    Join Date
    Nov 2003
    Location
    Connecticut
    Posts
    257
    Please forgive my ignorance, but I'm still not sure how to do that. My only experience is with simple exe programs.

  9. #9
    Fanatic Member holly's Avatar
    Join Date
    Aug 2002
    Location
    Somewhere on earth
    Posts
    721
    Try this
    VB Code:
    1. Private Sub Text1_KeyPress(KeyAscii As Integer)
    2.    
    3. If (KeyAscii < 47 Or KeyAscii > 57) Then 'Disable keyboard lettering
    4.                   KeyAscii = 0
    5.                    
    6.      End If
    7.  
    8. end sub

    HTH
    ** HOLLY **

  10. #10

    Thread Starter
    Hyperactive Member
    Join Date
    Nov 2003
    Location
    Connecticut
    Posts
    257
    I tried your code, but that still allows me to type anything I want into the text box.

  11. #11
    Fanatic Member holly's Avatar
    Join Date
    Aug 2002
    Location
    Somewhere on earth
    Posts
    721
    I have the same code in one of my app's and it works fine!

    are you change the procedure to whatever you have called your
    txt box...

    VB Code:
    1. Private Sub Text1_KeyPress(KeyAscii As Integer)
    2.  'here the text1 should be whatever you have called your txt box


    ** HOLLY **

  12. #12

    Thread Starter
    Hyperactive Member
    Join Date
    Nov 2003
    Location
    Connecticut
    Posts
    257
    Yes, I cut and pasted your code with the exception of the text box name. Here is my code:

    VB Code:
    1. Private Sub txtPNR_KeyPress(ByVal keyascii As Integer)
    2.  
    3.         If (keyascii < 47 Or keyascii > 57) Then
    4.             keyascii = 0
    5.         End If
    6.  
    7.  End Sub

    Still allows me to type anything into txtPNR. Numbers, letters, characters, etc.

  13. #13
    Former Admin/Moderator MartinLiss's Avatar
    Join Date
    Sep 1999
    Location
    San Jose, CA
    Posts
    33,431
    I don't know what you've done but the following from your code is invalid.

    Private Sub txtPNR_KeyPress(ByVal keyascii As Integer)

    The standard textbox KeyPress event sub header would look like

    Private Sub txtPNR_KeyPress(KeyAscii As Integer)

  14. #14
    Fanatic Member holly's Avatar
    Join Date
    Aug 2002
    Location
    Somewhere on earth
    Posts
    721
    as Marty says take out the Byval keyword.....Where in my code
    did you see byval...

    ** HOLLY **

  15. #15
    Lively Member
    Join Date
    Aug 2003
    Location
    everett washington
    Posts
    64

    snippet

    visual basic code:

    VB Code:
    1. Private Sub cmdSaveRecord_Click()
    2. If txtRegNo.Text = "" Then
    3.     MsgBox "Registration number must be entered"
    4.     txtRegNo.SetFocus
    5. Exit Sub
    6.    
    7. ElseIf Not IsNumeric(Me.txtRegNo) Then
    8.     MsgBox "You have entered a non-numeric value, please enter a number that is 5 digits in length", vbCritical, "input error"
    9.     Me.txtRegNo.SetFocus
    10. Exit Sub
    11.  
    12. ElseIf Len(Me.txtRegNo) < 5 Then
    13.     MsgBox "You have entered a number less than 5 digits. Enter only a 5-digit number", vbCritical, "input error"
    14.     Me.txtRegNo.SetFocus
    15. Exit Sub
    16. ElseIf txtFirstName.Text = "" Then
    17.     MsgBox "First name must be entered"
    18.     txtFirstName.SetFocus
    19. Exit Sub
    20. ElseIf txtLastName.Text = "" Then
    21.     MsgBox "Last name must be entered"
    22.     txtLastName.SetFocus
    23. Exit Sub
    24. End If
    25. 'When all data has been entered into text boxes and you press the command button (save record) it will be written and saved to a file for later viewing, if file isn't thier it will create it
    26. Open "C:\OffDataFile.txt" For Append As #1
    27. Write #1, UCase$(txtFirstName.Text), UCase$(txtLastName.Text), UCase$(txtRegNo.Text), UCase$(cboMonth.Text), UCase$(cboDay.Text), UCase$(cboYear.Text), UCase$(cboClass.Text)
    28.     txtFirstName.Text = ""   'data entered into these text boxes will be saved to a file
    29.     txtLastName.Text = ""
    30.     cboClass.Text = ""
    31.     txtRegNo.Text = ""
    32.     cboMonth.Text = ""
    33.     cboDay.Text = ""
    34.     cboYear.Text = ""
    35. Close #1
    36. End Sub
    this is more than what you wanted but it is good code to add to your collection!
    hope it helps
    Tarakwar

  16. #16
    Lively Member
    Join Date
    Aug 2003
    Location
    everett washington
    Posts
    64

    Unhappy oops

    sorry forgot to make it vb code.
    Tarakwar

  17. #17

  18. #18

    Thread Starter
    Hyperactive Member
    Join Date
    Nov 2003
    Location
    Connecticut
    Posts
    257
    I am using VB.net and when I type the following:

    private sub txtPNR_KeyPress (KeyAscii as Integer)

    and I press enter, the text automatically changes to:

    private sub txtPNR_KeyPress (ByVal KeyAscii as Integer)

    ??

  19. #19

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