Results 1 to 5 of 5

Thread: creating a procedure(resolved)

  1. #1

    Thread Starter
    Lively Member dedub's Avatar
    Join Date
    Dec 2002
    Location
    NC
    Posts
    98

    creating a procedure(resolved)

    Hey all.

    I have this code to keep everything but numbers from being typed into a text box. (not mine) I have to call this code for several boxes. Instead of copy and pasting this several times I want to call it in a procedure. I created one but it didnot work. My guess is that I have to elaborate the procedure by passing something to it, like the key that was pressed instead of the number. Anyway, could somone help out if you understand. Thanks!

    Code:
    Private Sub Text2_KeyPress(KeyAscii As Integer)
    If (KeyAscii > 0 And KeyAscii <= 32) Or (KeyAscii > vbKey0 And KeyAscii <= vbKey9) Then
    Exit Sub
    Else
    KeyAscii = 0
    MsgBox "Please enter only numeric values"
    End If
    Last edited by dedub; Nov 2nd, 2003 at 09:37 PM.
    R.L.T.W. A+, NET+, CCNA

    Doin' my best

  2. #2
    The Devil crptcblade's Avatar
    Join Date
    Aug 2000
    Location
    Quetzalshacatenango
    Posts
    9,091
    VB Code:
    1. Private Sub txt_KeyPress(KeyAscii As Integer)
    2.  
    3.     If (KeyAscii > 0 And KeyAscii <= 32) Or (KeyAscii > vbKey0 And KeyAscii <= vbKey9) Then
    4.         Exit Sub
    5.     Else
    6.         KeyAscii = 0
    7.         MsgBox "Please enter only numeric values"
    8.     End If
    9.  
    10. End Sub
    11.  
    12. Private Sub Text1_KeyPress(KeyAscii As Integer)
    13.     Call txt_KeyPress(KeyAscii)
    14. End Sub
    Laugh, and the world laughs with you. Cry, and you just water down your vodka.


    Take credit, not responsibility

  3. #3
    Former Admin/Moderator MartinLiss's Avatar
    Join Date
    Sep 1999
    Location
    San Jose, CA
    Posts
    33,431
    Here you go.

    VB Code:
    1. Public Function TestKey(KeyAscii)
    2.     If (KeyAscii > 0 And KeyAscii <= 32) Or (KeyAscii > vbKey0 And KeyAscii <= vbKey9) Then
    3.         TestKey = True
    4.     Else
    5.         KeyAscii = 0
    6.         TestKey = False
    7.     End If
    8.  
    9. End Function
    10.  
    11. Private Sub Text1_KeyPress(KeyAscii As Integer)
    12.  
    13.     If Not TestKey(KeyAscii) Then
    14.         MsgBox "Please enter only numeric values"
    15.     End If
    16.    
    17. End Sub
    You can also download the code for my NuberBox by clicking the link in my signature.

  4. #4

    Thread Starter
    Lively Member dedub's Avatar
    Join Date
    Dec 2002
    Location
    NC
    Posts
    98
    Wow, that was quick and that did it. I knew it would be something simple. I was forgetting to put the (KeyAscii) in my call. Thanks a bunch!
    R.L.T.W. A+, NET+, CCNA

    Doin' my best

  5. #5

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