Results 1 to 3 of 3

Thread: textbox default value

  1. #1
    VBAX Tutor
    Joined
    Dec 2008
    Posts
    244
    Location

    textbox default value

    I have 28 textbox that allows user to input their keyword to search. The default value for this is "Enter Keyword".

    I want to make it so that when they click inside this textbox, the default goes away so they can enter they're new keyword, but if they exit the textbox without entering anything, the default keyword "Enter Keyword comes back up.

    I did the first textbox with an if statement: I was wondering if there's a better way to do this than write 56 different if statement.

    Thanks

    [VBA]
    Private Sub TextBox1_Enter()
    If TextBox1.Value = "Enter Keyword" Then
    TextBox1.Value = ""
    End If
    End Sub
    Private Sub TextBox1_Exit(ByVal Cancel As MSForms.ReturnBoolean)
    If TextBox1.Value = "" Then
    TextBox1.Value = "Enter Keyword"
    End If
    End Sub
    [/VBA]

  2. #2
    Knowledge Base Approver VBAX Guru GTO's Avatar
    Joined
    Sep 2008
    Posts
    3,368
    Location
    Greetings,

    Not sure if any real advantage efficiency wise, as the tests are quick, but if I was to try, maybe (for 4 textboxes and a commandbutton):

    [vba]Option Explicit

    Private Sub CommandButton1_Click()
    Unload Me
    End Sub

    Private Sub TextBox1_Enter()
    TextBox_SetVal TextBox1
    End Sub

    Private Sub TextBox1_Exit(ByVal Cancel As MSForms.ReturnBoolean)
    TextBox_SetVal TextBox1, True
    End Sub

    Private Sub TextBox2_Enter()
    TextBox_SetVal TextBox2
    End Sub

    Private Sub TextBox2_Exit(ByVal Cancel As MSForms.ReturnBoolean)
    TextBox_SetVal TextBox2, True
    End Sub

    Private Sub TextBox3_Enter()
    TextBox_SetVal TextBox3
    End Sub

    Private Sub TextBox3_Exit(ByVal Cancel As MSForms.ReturnBoolean)
    TextBox_SetVal TextBox3, True
    End Sub

    Private Sub TextBox4_Enter()
    TextBox_SetVal TextBox4
    End Sub

    Private Sub TextBox4_Exit(ByVal Cancel As MSForms.ReturnBoolean)
    TextBox_SetVal TextBox4, True
    End Sub

    Private Function TextBox_SetVal(oTxtBox As MSForms.TextBox, _
    Optional Exiting As Boolean = False) As String
    If Exiting And oTxtBox.Value = vbNullString Then
    oTxtBox.Value = "Enter Keyword"
    ElseIf Not Exiting And oTxtBox.Value = "Enter Keyword" Then
    oTxtBox.Value = vbNullString
    End If
    End Function

    Private Sub UserForm_Initialize()
    Dim ctl As Control
    For Each ctl In UserForm1.Controls
    If ctl.Name Like "TextBox*" Then ctl.Value = "Enter Keyword"
    Next
    End Sub
    [/vba]

    Sometimes .Tag is a handy way to pick a control, but what I think I have brain-faded on is that I thought there was a .Type or similar??? (Hopefully if so, someone will smack me back awake)

    Anyways, hope this helps a bit and have a great day,

    Mark

  3. #3
    VBAX Tutor
    Joined
    Dec 2008
    Posts
    244
    Location
    thanks for you help, i'll explore this a bit more.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •