Re: Format Phone Number In TextBox
It's sort of a combination of what you started with, and the format code that Dave provided. You ultimately want to end up with Format(TextBox126.value, "00000 000 000") or Format(TextBox126.value, "##### ### ###")...either one will work fine, I chose to use "#". However, if you simply format all entries that way, I found that it does strange and unwanted things as you're typing in numbers. So, you have to create the format to match the number of digits being entered. In this loop:
For i = 1 To Len(Replace(TextBox126.Value, " ", ""))
FormatString = FormatString & "#"
If i = 5 Or i = 8 Then
FormatString = FormatString & " "
End If
Next i
for each digit in textbox126.value it will add a "#" to formatstring, with a space entered after digit 5 and digit 8. For example, if "123" is entered the format string will be "###"...and if "123456" is entered the format string will be "##### #".
The resulting format string is then used in the last line of the code:
TextBox126.Value = Format(Replace(TextBox126.Value, " ", ""), FormatString)
The Replace function used throughout the subroutine removes any spaces in textbox.value created by previous formatting or typed by the user.
At the beginning I added this line so that you could use backspace. It bails out at digits 5 and 8 so that the space isn't added continuously.
If Len(Replace(TextBox126.Value, " ", "")) = 5 Or Len(Replace(TextBox126.Value, " ", "")) = 8 Then Exit Sub
Hope this helped.