+ Reply to Thread
Results 1 to 9 of 9

Add a special format to a textbox to work while typing

  1. #1
    Forum Contributor
    Join Date
    06-07-2014
    Posts
    213

    Add a special format to a textbox to work while typing

    If you open the userform in the attached workbook, you will see a textbox with the word "OPERATOR". This word is fixed and cannot be deleted as per the code which enables it. After that word, the textbox has visible space for 7 more characters. This means that the word "OPERATOR" and 7 characters can be visible in the box at the same time.

    What I want is to have three numbers after the word, followed by a hyphen and then three further numbers. Therefore for example, I can have OPERATOR123-456 in the textbox. I want to have VBA that would format the box as the user types and automatically add the hyphen after the first three numbers perhaps. Finally, after the 6th number (i.e. the 7th character counting the hyphen) is entered, VBA code should prevent the user from entering any further characters in that box.

    I am not looking for an Exit routine as I would like the box to be formatted as one types.

    Any clue how to code this?
    Attached Files Attached Files

  2. #2
    Administrator 6StringJazzer's Avatar
    Join Date
    01-27-2010
    Location
    Tysons Corner, VA, USA
    MS-Off Ver
    MS365 Family 64-bit
    Posts
    24,721

    Re: Add a special format to a textbox to work while typing

    Here is one solution. Note that when you are processing raw user input there are tons of things the user can do wrong. I have caught most of them here, although the user will have problems if he has entered numbers past the hyphen and then tries to backspace over it.
    Attached Files Attached Files
    Jeff
    | | |會 |會 |會 |會 | |:| | |會 |會
    Read the rules
    Use code tags to [code]enclose your code![/code]

  3. #3
    Forum Contributor
    Join Date
    06-07-2014
    Posts
    213

    Re: Add a special format to a textbox to work while typing

    Quote Originally Posted by 6StringJazzer View Post
    Here is one solution. Note that when you are processing raw user input there are tons of things the user can do wrong. I have caught most of them here, although the user will have problems if he has entered numbers past the hyphen and then tries to backspace over it.
    This solution is spot on! I like the way you made it possible for someone to delete before the hyphen (instead of backspace) and start over the data entry if something needs changing. Good job on the solutions for possible scenarios too!

    Thanks a lot!

  4. #4
    Administrator 6StringJazzer's Avatar
    Join Date
    01-27-2010
    Location
    Tysons Corner, VA, USA
    MS-Off Ver
    MS365 Family 64-bit
    Posts
    24,721

    Re: Add a special format to a textbox to work while typing

    You didn't ask me any questions but there is a coding technique here that is essential for things like this. As part of the validation, the code may change the content of the textbox. When that happens it will fire the TextBox1_Change event, causing a recursive call. This gets very sticky. So if the code is changing the textbox, it sets a flag to say "The code is making this change, not the user." So at the beginning of the Sub, you check the flag, and if it's code making the change then you just exit.

  5. #5
    Forum Contributor
    Join Date
    06-07-2014
    Posts
    213

    Re: Add a special format to a textbox to work while typing

    That is really handy to know, 6StringJazzer, in the future; always good to know the theory behind these things! Thank you
    Last edited by chin67326; 01-09-2016 at 12:58 AM.

  6. #6
    Forum Contributor
    Join Date
    06-07-2014
    Posts
    213

    Re: Add a special format to a textbox to work while typing

    Quote Originally Posted by 6StringJazzer View Post
    You didn't ask me any questions but there is a coding technique here that is essential for things like this. As part of the validation, the code may change the content of the textbox. When that happens it will fire the TextBox1_Change event, causing a recursive call. This gets very sticky. So if the code is changing the textbox, it sets a flag to say "The code is making this change, not the user." So at the beginning of the Sub, you check the flag, and if it's code making the change then you just exit.
    What I should have also asked is, what if I wanted to have the same validation for the textbox but without the word "OPERATOR", how would the code look?

  7. #7
    Forum Contributor
    Join Date
    06-07-2014
    Posts
    213

    Re: Add a special format to a textbox to work while typing

    Any clue 6StringJazzer?

  8. #8
    Administrator 6StringJazzer's Avatar
    Join Date
    01-27-2010
    Location
    Tysons Corner, VA, USA
    MS-Off Ver
    MS365 Family 64-bit
    Posts
    24,721

    Re: Add a special format to a textbox to work while typing

    In the first line of code where the prefix is defined, change

    "OPERATOR"

    to

    ""

  9. #9
    Forum Contributor
    Join Date
    06-07-2014
    Posts
    213

    Re: Add a special format to a textbox to work while typing

    Sounds good; thanks!

+ 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. [SOLVED] Excel 2010 - Userform - display date from textbox in a label or textbox in 'ddd' format
    By theshybutterfly in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 03-29-2015, 10:54 PM
  2. cant keep typing in userform textbox after change event
    By jed38 in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 08-01-2014, 09:13 AM
  3. [SOLVED] Datepicker, textbox & cells date-format does not match for formula to work
    By KK1234 in forum Excel Programming / VBA / Macros
    Replies: 7
    Last Post: 05-02-2014, 10:04 AM
  4. unlock textbox for typing
    By Isabel Lopatin in forum Excel General
    Replies: 3
    Last Post: 01-31-2013, 12:33 PM
  5. Restricting typing of special characters
    By flakedew in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 06-20-2012, 11:26 AM
  6. Apply currency format to textbox while typing
    By kuraitori in forum Excel Programming / VBA / Macros
    Replies: 10
    Last Post: 03-12-2012, 11:09 PM
  7. Stopping user from typing in textbox
    By dextras in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 07-04-2009, 06:49 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