So, I have been able to piece together code from a few different searches to create a TextBox that mimics the masking functionality in Access, where only numeric values are accepted into the underscored sections of the "mask" text the textbox starts out populated with.
My desired data format for the entered value is a 4 digit number, a dash, and a single digit number (the single digit will usually, but not always, be a "1"):
Mask text:
____-_
Sample allowed values:
0001-1
0023-1
0106-2
1002-1
0074-1
etc...
It's working, but I would like the ability to backspace one character at a time, and the code (in sample file version 1) resets the whole field to the default "mask" text on a single "backspace" (or "delete") keystroke.
Here's the code:
I pretty much follow what's going on in the code, and figured out that if I make the following changes to the KeyDown event sub (as seen in sample file version 2), the backspace key works correctly until you get to the "-", and then it only works on the single digit after the "-". Also, if you type in 2 or more digits and then click in between them, the backspace acts real goofy, inserting "-"s where they shouldn't be and just generally wreaking all kinds of havok, and without leaving the "If keycode = 46..." statement to allow the delete key to function, you can't even clear it out and start over. It's a mess.Please Login or Register to view this content.
My experimental changes:
Is there a way to have the backspace key correctly back up one space and replace the digit with an underscore, regardless of where it is in the string, skipping over the "-" in the same way that the cursor jumps the "-" when typing the number in?Please Login or Register to view this content.
Bonus points if you can also add a msgbox that prevents exiting the textbox without typing all 5 digits in the number... Maybe on an exit event, with an If statement that checks for any "_" characters in the TBcutfile.Value string (I don't know how to do that check)? I could try to catch that in the error validation section of the full code, but a lock on the textbox at time of entry would be a little cleaner on the user end. And, I'd still need to know how to look for any "_"s in the string... But that might be a question for a separate thread, or something else I can google.
Thanks in advance!
Bookmarks