+ Reply to Thread
Results 1 to 5 of 5

Code help appreciated:following code to restrict character input

Hybrid View

  1. #1
    Forum Contributor
    Join Date
    12-16-2005
    Posts
    161

    Code help appreciated:following code to restrict character input

    I have multiple textboxes on multiple forms that use the following code to restrict character input. Is there a way to set something up in a standard module to avoid duplicating the code?

    Private Sub txtYr2_KeyPress(ByVal KeyAscii As MSForms.ReturnInteger)
    Select Case KeyAscii
    Case Asc("0") To Asc("9")

    Case Else
    KeyAscii = 0
    End Select
    End Sub

    Private Sub txtYr3_KeyPress(ByVal KeyAscii As MSForms.ReturnInteger)
    Select Case KeyAscii
    Case Asc("0") To Asc("9")

    Case Else
    KeyAscii = 0
    End Select
    End Sub

    Cheers Peter

  2. #2
    Valued Forum Contributor
    Join Date
    06-16-2006
    Location
    Sydney, Australia
    MS-Off Ver
    2013 64bit
    Posts
    1,394
    All you need to do is put the common code into a new subroutine, then call that subroutine from other routines.

    ie
    Sub RestrictInput()
    Select Case KeyAscii
    Case Asc("0") To Asc("9")

    Case Else
    KeyAscii = 0
    End Select
    end sub

    Private Sub txtYr2_KeyPress(ByVal KeyAscii As MSForms.ReturnInteger)
    RestrictInput
    end sub

    Private Sub txtYr3_KeyPress(ByVal KeyAscii As MSForms.ReturnInteger)
    RestrictInput
    End Sub


    The only thing you will need to be aware of, is that you will need all the code in the same module, or alternatively add the module name to the start of the routine ie module1.RestrictInput

    Matt

  3. #3
    Forum Contributor
    Join Date
    12-16-2005
    Posts
    161

    Thanks Mallycat

    Have tried your suggeStion which is very logical - however I can still enter any character in the form text field. The code runs without error - I must be doing something wrong.

    Peter

  4. #4
    Valued Forum Contributor
    Join Date
    06-16-2006
    Location
    Sydney, Australia
    MS-Off Ver
    2013 64bit
    Posts
    1,394
    I'm not 100% sure what you are trying to do, but I assume you need to pass your parameters to the subroutine. ie

    ByVal KeyAscii As MSForms.ReturnInteger

    I'm not really sure how to do this, but I would try the following

    Sub RestrictInput(ByVal KeyAscii As MSForms.ReturnInteger)
    Select Case KeyAscii
    Case Asc("0") To Asc("9")

    Case Else
    KeyAscii = 0
    End Select
    end sub

    Private Sub txtYr2_KeyPress(ByVal KeyAscii As MSForms.ReturnInteger)
    RestrictInput KeyAscii
    end sub

    Private Sub txtYr3_KeyPress(ByVal KeyAscii As MSForms.ReturnInteger)
    RestrictInput KeyAscii
    End Sub

  5. #5
    Forum Contributor
    Join Date
    12-16-2005
    Posts
    161

    Thanks

    Thanks Mallycat, that works fine!

    Cheers

    Peter

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1