+ Reply to Thread
Results 1 to 9 of 9

Freeze (undeleteable) a default value in textbox but be able to add more text in that box

Hybrid View

  1. #1
    Forum Contributor
    Join Date
    06-07-2014
    Posts
    213

    Freeze (undeleteable) a default value in textbox but be able to add more text in that box

    If one wants to have a textbox display a default value on initialize, one can simply type the value in the 'Text' field in the Properties tab for that textbox. The problem is that the text can be deleted by the user when the userform is initialized. Is there a way to use code to freeze the value in the textbox so the user cannot delete it? What is required however is for the user to add more text in that same textbox. Example, if one wants to freeze the text OPERATOR at that start of textbox so it cannot be deleted but allow the user to add more text in that box after the word OPERATOR, what is a good technique to use to achieve this?

  2. #2
    Forum Expert
    Join Date
    12-14-2012
    Location
    London England
    MS-Off Ver
    MS 365 Office Suite.
    Posts
    8,448

    Re: Freeze (undeleteable) a default value in textbox but be able to add more text in that

    The simplest solution is to use two text boxes. Side By Side

    Another is to change the first eight characters in the text box to Operator whenever the text box is modified.

    
    Private Sub TextBox1_Change()
    
    If Len(TextBox1.Value) > 8 Then
    
    TextBox1.Value = "Operator" & Right(TextBox1.Value, Len(TextBox1.Value))
    
    Else
    
    TextBox1.Value = "Operator"
    
    End If
    
    End Sub
    Last edited by mehmetcik; 12-26-2015 at 11:17 AM.
    My General Rules if you want my help. Not aimed at any person in particular:

    1. Please Make Requests not demands, none of us get paid here.

    2. Check back on your post regularly. I will not return to a post after 4 days.
    If it is not important to you then it definitely is not important to me.

  3. #3
    Forum Contributor
    Join Date
    06-07-2014
    Posts
    213

    Re: Freeze (undeleteable) a default value in textbox but be able to add more text in that

    Quote Originally Posted by mehmetcik View Post
    The simplest solution is to use two text boxes. Side By Side

    Another is to change the first eight characters in the text box to Operator whenever the text box is modified.

    
    Private Sub TextBox1_Change()
    
    If Len(TextBox1.Value) > 8 Then
    
    TextBox1.Value = "Operator" & Right(TextBox1.Value, Len(TextBox1.Value))
    
    Else
    
    TextBox1.Value = "Operator"
    
    End If
    
    End Sub
    Thanks for your help but the above code only returned multiple Operator words in the textbox when one tries to input data in the box and the form froze.

    I read somewhere that there is some hard pressed way to force data to remain in a single textbox. I will try to find where I saw that.

  4. #4
    Forum Expert shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2010, 2019
    Posts
    40,689

    Re: Freeze (undeleteable) a default value in textbox but be able to add more text in that

    If the initial text is fixed, why not put it in a label to the left of the textbox?
    Entia non sunt multiplicanda sine necessitate

  5. #5
    Forum Contributor
    Join Date
    06-07-2014
    Posts
    213

    Re: Freeze (undeleteable) a default value in textbox but be able to add more text in that

    Quote Originally Posted by shg View Post
    If the initial text is fixed, why not put it in a label to the left of the textbox?
    This would not work as an option as the prefix words need to be in the same box where further information will be added and the data from that textbox record will be saved and used in further tasks.

  6. #6
    Forum Expert shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2010, 2019
    Posts
    40,689

    Re: Freeze (undeleteable) a default value in textbox but be able to add more text in that

    The code that extracts text from the textbox could certainly prepend the text in the label.

  7. #7
    Forum Contributor
    Join Date
    06-07-2014
    Posts
    213

    Re: Freeze (undeleteable) a default value in textbox but be able to add more text in that

    Quote Originally Posted by shg View Post
    The code that extracts text from the textbox could certainly prepend the text in the label.
    True but, I was hoping there was a way to have it all in one box.

  8. #8
    Forum Expert
    Join Date
    12-14-2012
    Location
    London England
    MS-Off Ver
    MS 365 Office Suite.
    Posts
    8,448

    Re: Freeze (undeleteable) a default value in textbox but be able to add more text in that

    My Apologies I forgot to subtract 8.

    So now I've added another routine to prevent the user over typing "Operator".


    
    Private Sub TextBox1_Change()
    
    If Len(TextBox1.Value) > 8 Then
    
    TextBox1.Value = "Operator" & Right(TextBox1.Text, Len(TextBox1.Text) - 8)
    
    Else
    
    TextBox1.Value = "Operator"
    
    End If
    
    End Sub
    
    
    Private Sub TextBox1_MouseDown(ByVal Button As Integer, ByVal Shift As Integer, ByVal X As Single, ByVal Y As Single)
    If TextBox1.SelStart < 8 Then TextBox1.SelStart = 8
    End Sub
    
    Private Sub UserForm_Activate()
    TextBox1.Value = "Operator"
    End Sub

  9. #9
    Forum Contributor
    Join Date
    06-07-2014
    Posts
    213

    Re: Freeze (undeleteable) a default value in textbox but be able to add more text in that

    Quote Originally Posted by mehmetcik View Post
    My Apologies I forgot to subtract 8.

    So now I've added another routine to prevent the user over typing "Operator".


    
    Private Sub TextBox1_Change()
    
    If Len(TextBox1.Value) > 8 Then
    
    TextBox1.Value = "Operator" & Right(TextBox1.Text, Len(TextBox1.Text) - 8)
    
    Else
    
    TextBox1.Value = "Operator"
    
    End If
    
    End Sub
    
    
    Private Sub TextBox1_MouseDown(ByVal Button As Integer, ByVal Shift As Integer, ByVal X As Single, ByVal Y As Single)
    If TextBox1.SelStart < 8 Then TextBox1.SelStart = 8
    End Sub
    
    Private Sub UserForm_Activate()
    TextBox1.Value = "Operator"
    End Sub
    This is some neat code; thanks!

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. [SOLVED] Cell/Textbox with helping default text...
    By Bjourne in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 04-13-2012, 09:09 AM
  2. Replies: 6
    Last Post: 02-17-2012, 04:23 AM
  3. Default Date in TextBox
    By AnthonyWB in forum Excel Programming / VBA / Macros
    Replies: 10
    Last Post: 06-09-2011, 03:27 PM
  4. is userform textbox input by user default TEXT?
    By JohnSeito in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 04-01-2009, 07:46 AM
  5. How to specify non-default date format for a textbox
    By lottesfog in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 11-12-2007, 12:10 AM
  6. Default values in textbox
    By drifter in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 10-25-2006, 07:10 AM
  7. [SOLVED] Default in a textbox and change a module name
    By filo666 in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 04-22-2005, 04:06 PM
  8. Textbox disappears in freeze panes
    By Waititi in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 03-01-2005, 03:23 PM

Bookmarks

Posting Permissions

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

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1