Setting Focus On A Textbox And Preparing Default Text To Be Over Written By User Input

Ark68

Well-known Member
Joined
Mar 23, 2004
Messages
4,570
Office Version
  1. 365
  2. 2016
Platform
  1. Windows
On my userform, a combobox change event is triggered when the user changes the value from the dropdown. If the user selects "Other", this code is executed:

Rich (BB code):
Private Sub uf1cbx1_operatorini_Change()
    If uf1cbx1_operatorini.Value = "Other" Then
        With uf1lbl1_name
            .Locked = False
            .SetFocus
            .Text = "Surname, Given"
            .ForeColor = RGB(220, 220, 220)
            .Font.Italic = True
            .SelStart = 0
            .SelLength = Len(.Text)
        End With
    Else
        uf1lbl1_name = Application.WorksheetFunction.VLookup(uf1cbx1_operatorini.Value, Range("uf1rng_staff"), 2, False)
    End If
    opscount = opscount + 1
    If opscount = 4 Then
        uf1cbtn1_submit.Enabled = True
        MultiPage1.Enabled = False
    End If
End Sub

The code in purple effectively unlocks the previously locked textbox, sets the focus, gives it a default value (to be replaced by the user), changes the font to a grey colour and italicized and highlights the text allowing the user to over-write the default. Because the focus is set automatically to this text box, the user can start typing to over write the default text.

A change event changes the font to a more appropriate black and non-italicized font.
Rich (BB code):
Private Sub uf1lbl1_name_change()
    With uf1lbl1_name
        .Font.Italic = False
        .ForeColor = RGB(0, 0, 0)
    End With
End Sub

My issue comes from the error checking routine upon the exit event of this text. On exit, my code assess the entry to make sure it's not blank, that it's not the default, and that it takes the proper form (with a comma and space)

Rich (BB code):
Private Sub uf1lbl1_name_Exit(ByVal Cancel As MSForms.ReturnBoolean)
    If Len(uf1lbl1_name) = 0 Then
        With uf1lbl1_name
            .Text = "Surname, Given"
            MsgBox "Improper name entry." & Chr(13) & "'Surname, Given'", , "ERROR"
            .SetFocus
            .Font.Italic = True
            .SelStart = 0
            .SelLength = Len(uf1lbl1_name.Text)
        End With
    ElseIf InStr(uf1lbl1_name, ", ") < 1 Then
        With uf1lbl1_name
            .Text = "Surname, Given"
            MsgBox "Improper name entry." & Chr(13) & "'Surname, Given'", , "ERROR"
            .SetFocus
            .Font.Italic = True
            .SelStart = 0
            .SelLength = Len(uf1lbl1_name.Text)
        End With
    ElseIf uf1lbl1_name.Text = "Surname, Given" Then
        With uf1lbl1_name
            .Text = "Surname, Given"
            MsgBox "Improper name entry." & Chr(13) & "Please use a real name.", , "ERROR"
            .SetFocus
            .Font.Italic = True
            .SelStart = 0
            .SelLength = Len(uf1lbl1_name.Text)
        End With
    End If
End Sub

What I would like to do is mimic the behaviour of this textbox as is done initially on the combobox change. (ie set default value, change font, and highlight text for over writing). This existing code isn't living up to that expectation, specifically the highlighting in preparation for over writing. As it stands now, the default text is placed, the font applied, but the user has to click into the textbox, manually highlighting the text to delete it or over write it.

Any help would be greatly appreciated!!
 

Excel Facts

How to calculate loan payments in Excel?
Use the PMT function: =PMT(5%/12,60,-25000) is for a $25,000 loan, 5% annual interest, 60 month loan.
Instead of using SetFocus, set Cancel to True...

Code:
    If Len(uf1lbl1_name) = 0 Then
        Cancel = True

Hope this helps!
 
Upvote 0
Perfect! Appears to be doing the trick!
Thank you Domenic.
 
Last edited:
Upvote 0

Forum statistics

Threads
1,223,891
Messages
6,175,229
Members
452,621
Latest member
Laura_PinksBTHFT

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