Search code examples
excelvbatextbox

Clearing textbox upon click


I'm relatively new to VBA, and am working on a small database for my employer. I'm trying to create a text box that, when clicked, deletes the contents that I have put in it. For example, I have a text box that says "first" for them to put in the first name of a member. When they click it, I want my text to disappear so they can enter what they want. (The matter of it only executing once is easily solved with an if statement). I've taken a look at the following question, but neither answer worked for me;

Clear text in textbox upon clicking it

The following code did nothing

Private Sub TextBox1_GotFocus()
TextBox1.Text = ""
End Sub

And the following code give me an "Expected, end of statement" compilation error on 'Handles';

Private Sub txtNewNameHere_GotFocus() Handles txtNewNameHere.GotFocus
txtNewNameHere.Text = ""
End Sub

Not sure why either of those did not work for me, but, as I said, I'm relatively new to this. Any help greatly appreciated.


Solution

  • This will clear the text box on focus if it did not already have focus:

    Private Sub TextBox1_Enter()
        TextBox1.Value = ""
    End Sub
    

    It will not clear the text box every time it is clicked. Does that help?