+ Reply to Thread
Results 1 to 8 of 8

How to restrict textbox input with module?

  1. #1
    Forum Contributor
    Join Date
    03-31-2012
    Location
    Hong Kong
    MS-Off Ver
    Excel 2010
    Posts
    140

    How to restrict textbox input with module?

    I want to create a module to restrict any textbox input, how to do it with follow example (not work)?
    Please Login or Register  to view this content.

  2. #2
    Forum Expert mikerickson's Avatar
    Join Date
    03-30-2007
    Location
    Davis CA
    MS-Off Ver
    Excel 2011
    Posts
    6,229

    Re: How to restrict textbox input with module?

    You might use code like this. Note that the code must put a "%" as the rightmost character of the text box in the Initialize event.

    Please Login or Register  to view this content.
    _
    ...How to Cross-post politely...
    ..Wrap code by selecting the code and clicking the # or read this. Thank you.

  3. #3
    Forum Expert mikerickson's Avatar
    Join Date
    03-30-2007
    Location
    Davis CA
    MS-Off Ver
    Excel 2011
    Posts
    6,229

    Re: How to restrict textbox input with module?

    Another way would be to put the "%" in a label right next to the text box and use only the KeyPress code above.

  4. #4
    Forum Contributor
    Join Date
    03-31-2012
    Location
    Hong Kong
    MS-Off Ver
    Excel 2010
    Posts
    140

    Re: How to restrict textbox input with module?

    Thanks you for your reference, but I have more textbox with various input format. Can I set the modules for them like:
    module A for integer input only
    module B for decimal input only
    module C for percentage% input only
    module D for text input only
    module E for date input only
    with checking and Msgbox?

  5. #5
    Forum Expert mikerickson's Avatar
    Join Date
    03-30-2007
    Location
    Davis CA
    MS-Off Ver
    Excel 2011
    Posts
    6,229

    Re: How to restrict textbox input with module?

    You could do that with a Class Module. (Date input would be a challenge, most people find that three combo boxes is a better user interface than a text box.)

    About C, I would use a text box with a label rather than requiring the text box entry to end in "%".

    Let me put something together and get back to you.

  6. #6
    Forum Contributor
    Join Date
    03-31-2012
    Location
    Hong Kong
    MS-Off Ver
    Excel 2010
    Posts
    140

    Re: How to restrict textbox input with module?

    I used to follow code for date textbox input check. I have study how to use anyTextBox_KeyPress(ByVal KeyAscii) to restrict input content, have you any idea?
    Please Login or Register  to view this content.

  7. #7
    Forum Expert mikerickson's Avatar
    Join Date
    03-30-2007
    Location
    Davis CA
    MS-Off Ver
    Excel 2011
    Posts
    6,229

    Re: How to restrict textbox input with module?

    I think you might like this.
    This uses a custom class, clsTextBox. Every clsTextBox has a DataType
    DataTypes can be
    Number, Real, Integer, Letters, Alphanumeric and can be further modified as "Postitive Integer" "Upper Case Letters" "Non-Alphanumeric" "Non-Negative Real"
    "Percentage" and "Date" are also possible.
    If the user goes into a textBox that is type "Date" three combo boxes will be superimposed over the text box, the user selects the month,day and year from those combo boxes.

    "Percentage" can also be modified with "integer" "real" "positive" "non-negative" etc. But a text box that is dataType "percentage" must end with a "%", even when empty, even at the start.

    I much much prefer putting a label with "%" next to the data entry text box, with a numeric data type, rather than including the "%" in the text boxes value.

    Other than the Date combo boxes
    DataValidation is all controled from the IsValid UDF that is in the user form's code module.
    You can add other data types as you wish. There is no need to change any of the code in the clsTextBox module.

    The code in clsTextBox is mostly just passing properies through the class to the underlying text box and passing events
    Please Login or Register  to view this content.
    The bulk of the user form's code is the drivers for the Date combo boxes.
    In the Userform_Intialize event, you notice that first all the text boxes are assigned to clsTextBox objects which are put into the TextBoxes collection. Then the data type for those text boxes are assigned.

    Please Login or Register  to view this content.
    The AnyTextBox_KeyPress event handles data entry, the _KeyDown event handles the Delete Key, _KeyUp and _MouseUp are used for the special handling needed by Percent and Date text boxes.
    Attached Files Attached Files

  8. #8
    Forum Contributor
    Join Date
    03-31-2012
    Location
    Hong Kong
    MS-Off Ver
    Excel 2010
    Posts
    140

    Re: How to restrict textbox input with module?

    Thanks for your help, I will test and study your code in order to improve my project.

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

Similar Threads

  1. Populate TextBox from Module prior to opening TextBox
    By Dabbler39 in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 09-05-2013, 05:50 PM
  2. Restrict textbox entries to numbers
    By domnisignis18 in forum Excel Programming / VBA / Macros
    Replies: 8
    Last Post: 07-11-2013, 03:48 AM
  3. vba excel user form restrict input on user textbox
    By samz93 in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 04-04-2013, 04:38 PM
  4. restrict textbox on userform to mm/yyyy
    By lcoreilly in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 04-22-2010, 11:40 AM
  5. Restrict Textbox entry
    By pauluk in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 05-15-2007, 08:08 AM

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