Highlighting Userform Textbox Text To Type-Over

Ark68

Well-known Member
Joined
Mar 23, 2004
Messages
4,564
Office Version
  1. 365
  2. 2016
Platform
  1. Windows
I have this code which accepts a telephone number entry in a userform textbox (xtended_ci.p_tn1) from the user.

VBA Code:
       With p_tn1
            .Enabled = True
            .Locked = False
            .BackColor = clr_blue
            .Text = "###.###.####"
            .SetFocus
            .SelStart = 0
            .SelLength = 12
        End With

The code opens up the field for editing, highlights the field by applying a backcolor, and provides a mask of the data to be entered. I would like this default text to be overwritten by the user's entry without the user having to manually highlight the text. What I have doesn't appear to be the solution.

Open to improvements to make this work.
 

Excel Facts

Round to nearest half hour?
Use =MROUND(A2,"0:30") to round to nearest half hour. Use =CEILING(A2,"0:30") to round to next half hour.
Assuming your posted code for the textbox is triggered by the userform's Initialize event (or even if it is not), this can do what maybe you want but hard to say exactly what that is:

VBA Code:
Private Sub UserForm_Initialize() 
With  xtended_ci.p_tn1  
.SetFocus 
.SelStart = 0 
.SelLength = Len(.Text) 
End With 
End Sub
 
Upvote 0
Thank you Tom, I so appreciate your input. That really isn't any different that what I have and the only change I made (replacing .SelLength = 12 with .SelLength = Len(.text) made no difference. What is different is that my code isn't part of the initialization routine, it's part of another control's change event. Let me try to explain. I think it's much more simple than what some may be thinking, the concept is just being lost in explanation.

Users of the userform much enter data in a sequential order. So, all controls (fields) are disabled until which time the field preceding it has been accepted. In this case, before textbox p_tn1 can accept data from the user (enabled), the user has to enter data into the p_name textbox that precedes it. Putting your code inthe initialization event would seem fitting for the first and only control open to the user when the form first opens. Nothing else is accessible. Once that data is accepted, the afterupdate code of textbox p_name prepares the textbox p_tn1.
  • the enabled and locked parameters allow access to the field
  • the backcolor is changed as a visual indicator to the user where the focus of input is
  • the text provides the user an illustration as to the format expected for that field. It is not an acceptable value. The idea is the the user will overwrite that illustration with the actual value in that format
So, what is happening, is when the the p_name change code triggers, all the formatting of p_tn1 is happening. However, rather than the user being automatically taken the p_tn1, they have to manually move to that control. In doing so, they have to click in that textbox. Clicking in the textbox buts the cursor ahead of the ###.###.####. The user typing anything of course just inserts characters before it. I want those default characters written over by the users inpout. The only way to do that is for the user to manually select (highlight) the ###.###.#### to be overwritten. What I am really wanting to do, which this isn't for some reason, is that when the textbox t_pn1 takes focus (Setfocus??) from the previous textbox change event, that that selection (highlighting) is done automatically. The user can immediately begin typing over the default text without having to manually select it.

Here is my full afterupdate event code for textbox p_name which needs a value before granting access to textbox p_tn1.

VBA Code:
Private Sub p_name_afterupdate()
    pnm = p_name.Value
    If Len(pnm) = 0 Then 'blank entry
        MsgBox "A primary contact name must be provided." & Chr(13) & "{Surname, Given}", vbCritical, "Error: Customer Deficiency"
        cfc = cfc - 1
        p_name.BackColor = clr_red
    ElseIf InStr(pnm, ", ") = 0 Then 'improper format
        MsgBox "Names must be entered in the proper format." & Chr(13) & "{Surname, Given}", vbCritical, "Error: Customer Deficiency"
        mbevents = False
        p_name.Value = ""
        mbevents = True
        p_name.BackColor = clr_red
    Else 'name ok
        p_name.BackColor = vbWhite
        With p_tn1
            .Enabled = True
            .Locked = False
            .BackColor = clr_blue
            .Text = "###.###.####"
            .SetFocus
            .SelStart = 0
            .SelLength = Len(.Text)
        End With
    End If
    chk_cfc
End Sub

Here is
 
Upvote 0
perhaps try setting the p_tn1 EnterFieldBehavior property to 1,
and using the p_tn1_Change event to set up the selections
VBA Code:
Private Sub p_tn1_Change()
With Me.p_tn1
    x = InStr(p_tn1.Text, "#")
    If x = 0 Then Exit Sub  'move to next control here
    x = x - 1
    .SetFocus
    .SelStart = x
    .SelLength = 1
End With
End Sub
 
Upvote 0
Hi NoSparks, this is a great improvement. Thank you!
I'm wondering if it can be adapted so that it behaves the same when the textbox is entered by way of tabbing between controls. This works as requested, by the user clicking in the textbox, but many opt to just tab between controls.
 
Upvote 0
Maybe using the _Enter event to insert the phone mask would do
VBA Code:
Private Sub p_tn1_Enter()
    With Me.p_tn1
        If .Text = "" Then
            .Text = "###.###.####"
        End If
    End With
End Sub
 
Upvote 0
Nice! Is this in addition to the code you provided earlier? So I have an '_enter' event, a '_change' event and an '_afterupdate' event for thios control? I didn't realize I could have more than one event associated to a control.
 
Upvote 0
Just move the .SetFocus to after you have defined the selection.
VBA Code:
' .setfocus : not here
.SelStart = x
.SelLength = 1
.SetFocus: Rem yes here
 
Upvote 0
Thanks NoSparks and Mike.
As I've been trying to integrate this advice things seem to have gotten kind of messy. NoSparks, the code you provided in post #6 wrecked havoc with my p_tn1_afterupdate code. I removed it because I felt I could live without the function of using tab if it meant saving the headache of tracing down the problem.

With Mike's contribution, I assume his suggestion was a correction to NoSparks initial solution in post 4 and that the code in post 6 wouldn't be needed. Bad assumption?

So, I's running into some functionality issues with my code withthe introduction of the corrected post 4 code.

As mentioned in my OP, access to p_tn1 is made avilable only after a value is successfully entered in the preceding textbox, p_name. This is accomplished with this afterupdate event:

VBA Code:
rivate Sub p_name_afterupdate()
    Stop
    pnm = p_name.Value
    If Len(pnm) = 0 Then 'blank entry
        MsgBox "A primary contact name must be provided." & Chr(13) & "{Surname, Given}", vbCritical, "Error: Customer Deficiency"
        cfc = cfc - 1
        p_name.BackColor = clr_red
    ElseIf InStr(pnm, ", ") = 0 Then 'improper format
        MsgBox "Names must be entered in the proper format." & Chr(13) & "{Surname, Given}", vbCritical, "Error: Customer Deficiency"
        mbevents = False
        p_name.Value = ""
        mbevents = True
        p_name.BackColor = clr_red
    Else 'name ok
        p_email.Enabled = True
        p_name.BackColor = vbWhite
        With p_tn1
            .Enabled = True
            '.Locked = False
            .BackColor = clr_blue
            mbevents = False
            .Text = "###.###.####"
            mbevents = True
            '.SetFocus
            '.SelStart = 0
            '.SelLength = Len(.Text)
        End With
    End If
    chk_cfc
End Sub

I'm trying to find a solution that if the user tabs out of p_name, the afterupdate event is successful, and it sends the user to p_tn1 withthe mask (###.###.####) highlighted to be overwritten by the user's submission.

So, I added this code:
VBA Code:
Private Sub p_tn1_Change()
'Stop
    With Me.p_tn1
        X = InStr(p_tn1.Text, "#")
        If X = 0 Then Exit Sub  'move to next control here
        X = X - 1
        .SelStart = X
        .SelLength = 1
        .SetFocus
    End With
End Sub

So the issue I'm encountering now, is that when the user tabs out of p_name, my p_tn1 afterupdate code kicks in and treats it as an erroronous entry before even starting.

VBA Code:
Private Sub p_tn1_afterupdate()
    str_p_tn1 = p_tn1.Value
    mbevents = False
    If Len(str_p_tn1) <> 12 Then
        MsgBox "Please enter a proper telephone number." & Chr(13) & "{###.###.####}", vbInformation, "ERROR: Telephone Format"
        p_tn1.BackColor = clr_red
        p_tn1.Value = "###.###.####"
    End If
    If Mid(str_p_tn1, 4, 1) <> "." Or Mid(str_p_tn1, 8, 1) <> "." Then
        MsgBox "Please enter a proper telephone number." & Chr(13) & "{###.###.####}", vbInformation, "ERROR: Telephone Format"
        p_tn1.BackColor = clr_red
        p_tn1.Value = "###.###.####"
    End If
    a1 = Left(str_p_tn1, 3)
    a2 = Mid(str_p_tn1, 4, 3)
    a3 = Right(str_p_tn1, 4)
    If IsNumeric(a1) = False Or IsNumeric(a2) = False Or IsNumeric(a3) = False Then
        MsgBox "Please enter a proper telephone number." & Chr(13) & "{###.###.####}", vbInformation, "ERROR: Telephone Format"
        p_tn1.BackColor = clr_red
        p_tn1.Value = "###.###.####"
    End If
    mbevents = True
    
    p_tn1.BackColor = vbWhite
    p_email.Enabled = True
    p_tn1_c.Enabled = True
    p_tn1_b.Enabled = True
    p_tn1_h.Enabled = True
    
    chk_cfc
    
End Sub[/code[

User enters valid name into p_name, hits tab, msgbox pops up "Please enter a proper telephone number.", exit msgbox, p_tn1 is populated with the mask and the user can overwrite it, but the backcolor isn't blue.

I can step through the code line by line starting with the p_name afterupdate event right through to the formatting and awaiting of user overwrite into p_tn1 without any odditities. But in runtime, I get this issue. 

Am I missing something perhaps?
 
Upvote 0

Forum statistics

Threads
1,214,826
Messages
6,121,792
Members
449,048
Latest member
greyangel23

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top