Highlighting Userform Textbox Text To Type-Over

Ark68

Well-known Member
Joined
Mar 23, 2004
Messages
4,562
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.
 
On further testing, the p_tn1_afterupdate event is being triggered when the text property is set to "###.###.####" in the p_name afterupdate event code.
I have mbevents = false before it to disable any events associated with that control changing, but makes no difference. Also changed that to application.enablevents = false but it didn't make a difference either.
 
Upvote 0

Excel Facts

Ambidextrous Undo
Undo last command with Ctrl+Z or Alt+Backspace. If you use the Undo icon in the QAT, open the drop-down arrow to undo up to 100 steps.
"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?"
My contribution was to the OP code.
 
Upvote 0
Sorry, I can't set up what you've got in order to step through and see exactly what's going on, so afraid I'm not much help
but for afterupdate to be triggered you must be leaving the text box some how after writing to it.
 
Upvote 0
Thanks guys, I really do appreciate the effort
I've adjusted my original code to reflect therecommendation Mike made, and being under the impression that NoSparks contributions wouldn't be necessary as a result, removed them.

Code:
With p_tn1
            .Enabled = True
            '.Locked = False
            .BackColor = clr_blue
            .Text = "###.###.####"
            .SelStart = 0
            .SelLength = Len(.Text)
            .SetFocus
        End With

I can't test it because I am unable to get past the premature triggering of the p_tn1 afterupdate code.
I have narrowed the culprit down to the line highlighted in red above as the trigger. If I remove that line, the p_tn1 afterupdate event doesn't trigger and things advance as expected minus the automatically selected "###.###.####" default text. I was surprised my efforts to surpress events (mbevents and application.events) didn't prevent the triggering of the afterupdate event.

I would love nmo more than to share my workbook, but it's much too complicated and would take me longer to censor it that what the benefit would be. I may just have to resort to giving up LOL.
 
Upvote 0
Oh well, the original request was for highlighting to type-over which you did get.
In my mind this should be simple to implement, unfortunately the ins-and-outs of your user form don't agree.
Hopefully something will come to light that will allow for its use.

Good luck with your project.
 
Upvote 0
Application.Enableevents doesn't apply to userforms. Your Boolean variable won't prevent the event from triggering, but should be able to prevent it from actually doing anything, assuming you used it properly.
 
Upvote 0
Thanks for the support guys!
assuming you used it properly
And there was the nudge I needed! Thanks Rory!!

Now that that issue has been resolved, I can now test for the original problem.
With MikeRickson's input, my code to format and set focus to textbox p_tn1 is:

Code:
Private 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
        mbevents = False
        With p_tn1
            .Enabled = True
            '.Locked = False
            .BackColor = clr_blue
            .Text = "###.###.####"
            .SelStart = 0
            .SelLength = Len(.Text)
            .SetFocus
        End With
        mbevents = True
    End If
    chk_cfc
End Sub

When this code executes, p_tn1 formats properly, the mask is in place, but isn't highlighted. The user can use the mouse and click on the control which will highlight the mask to be overwritten. That's acceptable, but preferred if it was automatically highlighted for the user to simply type their value (no clicking, do manual highlighting)
 
Upvote 0
what happens if you use .SelLength = 1 instead of the entire text ?
 
Upvote 0
I am teetering on the edge of giving up guys. It shouldn't be this complicated I'm sure you're all saying. My last hope is to share the userform and see if it is behaving the same for you as it is for me.

If I ask politely, will someone try this form out and see if they can narrow down a cause.

Run the form, and enter a name in the format "surname, given". It won't let you proceed if you enter something different. A proper entry will enable the next textbox for telephone number entry.

After the user enters the name, and presses tab or enter, the preferred action will be for the telephone textbox (p_tn1) to turn blue, and have an automatically selected (highlighted) ###.###.#### mask ready for the user to type over.

What is happening (for me), is after the tab or enter is pressed, notice is given that the telephone number is incorrect. This message comes from the p+tn1 afterupdate event which checks for a "proper" telephone entry. Dismissing the message results in the textbox being populated by the mask, but no blue background. This code should be surpressed by the mbevents = false / if not mbevents then exit sub code. But its not. The afterupdate code I think is assessing the initial setting of the text parameter despite measures in place to bypass that assessment. "###.###.####" is not a proper telephone number.

If I step through the code, everything works (except the automatic selection of the mask. The user needs to click the control to have the mask selected). But the same code in run time has the undesired behaviour.

Thank you all for your help.
 
Upvote 0

Forum statistics

Threads
1,214,377
Messages
6,119,185
Members
448,872
Latest member
lcaw

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