ControlTipText on textbox in userform

leopardhawk

Well-known Member
Joined
May 31, 2007
Messages
611
Office Version
  1. 2016
Platform
  1. Windows
Hello forum friends,

I am trying to discover if there is a way to have greyed-out text (perhaps some instructions) appear in a text box on a UserForm that the user could 'over-type' as they fill in the text box, similar to what you see in a webpage? I don't like the idea of using ControlTipText as that method doesn't manifest unless the user actually hovers over the text box. Appreciate any suggestions or help. Thanks!
 

Excel Facts

Copy formula down without changing references
If you have =SUM(F2:F49) in F50; type Alt+' in F51 to copy =SUM(F2:F49) to F51, leaving the formula in edit mode. Change SUM to COUNT.
I am away from MS Forms for a long time, but I think they still don't provide placeholder text (ghost text may be?) in the text box controls.

My suggestion would be:

  1. Create a text box control, and set its Tag property as the placeholder text you wish, "Enter your name" for the name field may be.
  2. And use the following code to set it up according to its state and content by using Enter and Exit events of the text box.
  3. Also call the function in form init, so the text box shows the placeholder text if it is not the first control which has the focus.

Note: I used a separate procedure, setupPlaceholder, so you can call it for all text boxes on the user form that you'd like to use placeholder text.
Note2: All code below goes to the Userform module, however, you can move the setupPlaceholder into a standard module to use it with multiple user forms. You'll need to pass the user form name reference as well in that case. I am trying to say, it could be extended as necessary if you like this method.

VBA Code:
Private Sub UserForm_Initialize()
    setupPlaceholder TextBox1.Name, False
End Sub

Private Sub TextBox1_Enter()
    setupPlaceholder ActiveControl.Name, True
End Sub

Private Sub TextBox1_Exit(ByVal Cancel As MSForms.ReturnBoolean)
    setupPlaceholder ActiveControl.Name, False
End Sub

Sub setupPlaceholder(txtBox As String, focus As Boolean)
    With Me.Controls(txtBox)
        If Len(.Text) = 0 And Not focus Then
            .Text = .Tag
            .ForeColor = vbGrayText
        ElseIf .Text = .Tag Then
            .Text = ""
            .ForeColor = vbWindowText
        End If
    End With
End Sub
 
Upvote 0
@smozgur thanks for reaching out. I will be attempting to work this into my existing code and report back after I have a chance to test it.
 
Upvote 0
@smozgur hello again, I am running into difficulties with the UserForm_Initialize and continue to receive a compile error. Below is my existing code and I can't figure out how to integrate your suggestion into the code. Also, the 'textbox(s)' that I am concerned with setting this up for are called DOBTextBox, RDTextBox, CPPTextbox and OASTextBox. Hope you can steer me in the right direction.
Code:
Private Sub UserForm_Initialize()

'Set Focus
Me.FirstNameTextBox.SetFocus

'Empty FirstNameTextBox
FirstNameTextBox.Value = ""

'Empty LastNameTextBox
LastNameTextBox.Value = ""

'Empty DOBTextBox
DOBTextBox.Value = ""

'Empty GenderComboBox
GenderComboBox.Clear

'Fill GenderComboBox
With GenderComboBox
    .AddItem "M"
    .AddItem "F"
End With

'Empty CompanyTextBox
CompanyTextBox.Value = ""

'Empty RDTextBox
RDTextBox.Value = ""

'Empty OptionComboBox
OptionComboBox.Clear

'Fill OptionComboBox
With OptionComboBox
    .AddItem "100% Joint Life"
    .AddItem "60% Joint Life 5-year guarantee"
    .AddItem "60% Joint Life 10-year guarantee"
    .AddItem "60% Joint Life 15-year guarantee"
    .AddItem "Single Life no guarantee"
    .AddItem "Single Life 5-year guarantee"
    .AddItem "Single Life 10-year guarantee"
    .AddItem "Single Life 15-year guarantee"
End With

'Empty ProviderComboBox
ProviderComboBox.Clear

'Fill ProviderComboBox
With ProviderComboBox
    .AddItem "College"
    .AddItem "Municipal"
    .AddItem "Public Service"
    .AddItem "Teachers'"
    .AddItem "WorkSafeBC"
    .AddItem "Other"
End With

'Empty CPPTextBox
CPPTextBox.Value = ""

'Empty OASTextBox
OASTextBox.Value = ""

End Sub
 
Upvote 0
The Compile error should be addressing to a missing object in your user form. Take a look at where it stops execution, and make sure you typed the control name correctly where execution stops (highlighted line when error happens and you click on Debug).

Honestly, I don't see any reason to clear the combo box controls, and text box values since all will be cleared during the form initialization already.
And, my preference to fill the combo box items would be using the List property instead of executing .AddItem method unless the values are dynamic.

So, following should work without problems.

VBA Code:
Private Sub UserForm_Initialize()
Dim objControl As MSForms.Control

    ' This is to initialize placeholder text for each text box
    ' Make sure setting Tag property of the text box controls
    ' They will be the placeholder text
    ' And also make sure you create Enter and Exit event procedures for all text boxes
    ' as I created those only for FirstNameTextBox
    For Each objControl In Me.Controls
        If TypeName(objControl) = "TextBox" And objControl.Tag <> "" Then
           Me.setupPlaceholder objControl.Name, False
        End If
    Next objControl

    ' Fill the combo box list items
    Me.GenderComboBox.List = Array("M", "F")
    
    Me.OptionComboBox.List = Array("100% Joint Life", _
        "60% Joint Life 5-year guarantee", _
        "60% Joint Life 10-year guarantee", _
        "60% Joint Life 15-year guarantee", _
        "Single Life no guarantee", _
        "Single Life 5-year guarantee", _
        "Single Life 10-year guarantee", _
        "Single Life 15-year guarantee")
    
    Me.ProviderComboBox.List = Array("College", _
        "Municipal", _
        "Public Service", _
        "Teachers'", _
        "WorkSafeBC", _
        "Other")
    
    ' You don't need the following code to set the focus during the form initialization
    ' Just set the TabIndex property of this control as 0
    ' Me.FirstNameTextBox.SetFocus
    
End Sub

Sub setupPlaceholder(txtBox As String, focus As Boolean)
    With Me.Controls(txtBox)
        If Len(.Text) = 0 And Not focus Then
            .Text = .Tag
            .ForeColor = vbGrayText
        ElseIf .Text = .Tag Then
            .Text = ""
            .ForeColor = vbWindowText
        End If
    End With
End Sub

' You need to create the following events for all textboxes
' that you need placeholder text
' I just created it for FirstNameTextBox control
' So it will only work when you enter to or exit from this text box
Private Sub FirstNameTextBox_Enter()
    setupPlaceholder ActiveControl.Name, True
End Sub
Private Sub FirstNameTextBox_Exit(ByVal Cancel As MSForms.ReturnBoolean)
    setupPlaceholder ActiveControl.Name, False
End Sub

Note: I would normally create a class module to use single event procedure for all text boxes for Enter and Exit events, however for some reason that I don't remember, Enter and Exit events are not available for this implementation. I insisted and also tried to use Control object, but it didn't work either. Either I am missing something, or these events are not exposed for class extension. It only causes you to create Enter and Exit events for each boxes, that's all.
 
Upvote 0
Solution
@smozgur thank you. I will again try to integrate your code into mine and let you know the results. I really appreciate your help with this.
 
Upvote 0
@smozgur okay, I think this is a step in the right direction but I am getting an error. Your code puts "Enter your first name" in FirstNameTextBox in the color gray which is all good but as soon as I click my cursor in that field to enter a name, I get a run-time error 438 and clicking 'debug' highlights in yellow that line of your code that reads "If Len(.Text) = 0 And Not focus Then".
 
Upvote 0
Could you please try passing the actual control name instead of using the ActiveControl.Name?

Change the Enter and Exit event procedures with the following:
VBA Code:
' You need to create the following events for all textboxes
' that you need placeholder text
' I just created it for FirstNameTextBox control
' So it will only work when you enter to or exit from this text box
Private Sub FirstNameTextBox_Enter()
    setupPlaceholder FirstNameTextBox.Name, True
End Sub
Private Sub FirstNameTextBox_Exit(ByVal Cancel As MSForms.ReturnBoolean)
    setupPlaceholder FirstNameTextBox.Name, False
End Sub
 
Upvote 0
@smozgur thank you so much! It works now after that modification. I am wondering if there is a way to have the cursor in the name field (the first field) on the form when it is first opened? I do have the tab index at 0 and all subsequent fields in order from 1-11 but the cursor does not show up in the first field unless I hit the TAB key. Thanks!
 
Upvote 0
There must be something else taking the focus when you open the user form, but it can be only initiated in the code. Otherwise, TabIndex = 0 should work without problem.

Please enable the Me.FirstNameTextBox.SetFocus in the code. It will make sure setting the focus on the user form.
 
Upvote 0

Forum statistics

Threads
1,214,590
Messages
6,120,423
Members
448,961
Latest member
nzskater

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