+ Reply to Thread
Results 1 to 6 of 6

Restricting data input in multiple textboxes

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

    Restricting data input in multiple textboxes

    I have 64 textboxes on a userform. I want to restrict input to numeric values only. Have found some code that works as follows:

    Public Sub TextBox1_KeyPress (ByVal KeyAscii As MSForms.ReturnInteger)
    Select Case KeyAscii
    Case Asc("0") To Asc("9")
    Case Else
    KeyAscii = 0
    End Select
    End Sub

    Question is how can this code be writen without having to create 64 entries?

    Any help much appreciated.

    Cheers

    PeterT

  2. #2
    Forum Contributor funkymonkUK's Avatar
    Join Date
    01-07-2005
    Location
    London, England
    Posts
    500
    Quote Originally Posted by peter.thompson
    I have 64 textboxes on a userform. I want to restrict input to numeric values only. Have found some code that works as follows:

    Public Sub TextBox1_KeyPress (ByVal KeyAscii As MSForms.ReturnInteger)
    Select Case KeyAscii
    Case Asc("0") To Asc("9")
    Case Else
    KeyAscii = 0
    End Select
    End Sub

    Question is how can this code be writen without having to create 64 entries?

    Any help much appreciated.

    Cheers

    PeterT
    perhaps create a sub with the loop with the function in it?

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

    Still stuck with this

    Thanks for the reply. However am still unsure how to code this and make it work - any hel[ appreciated.

    CHeers

    PeterT

  4. #4
    Valued Forum Contributor
    Join Date
    08-26-2006
    Location
    -
    MS-Off Ver
    2010
    Posts
    388
    One way is to use a Class Module to handle the events for multiple controls.

    There is an example here: Toggle Button Events Class

    That article also has some useful links to the following pages:

    Handle Multiple UserForm Buttons With One Subroutine

    Handling Worksheet Control Events Using a Class Module

    The same principles can be applied to any controls on userforms.
    Using the pages above I got your example going with only one keypress event for 8 textboxes.

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

    Thanks T-J

    Thanks for the information, much appreciated! Could you post the code that you got to work? I'm still not quite there yet!

    Cheers

    PeterT

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

    Problem solved

    Now have code working fine. Thanks T-J.

    PeterT

+ 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