Results 1 to 6 of 6

Thread: User form textbox issue

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Jan 2010
    Posts
    21

    User form textbox issue

    Hi,

    I'm creating a user form textbox to display a large message because msgbox has character limitation.

    I created a user form, added a text box and changed the follwoing properties:
    MultiLine = True
    ScrollBars = 2 - fmScrollBarsVertical
    Width = 600
    WordWrap = True.

    In the vba code of this form I have the following:

    Code:
    Private Sub TextBox1_Change()
    
    TextBox1.Text = txt
    
    End Sub
    I have another sub which passes text to global variable txt and at the end shows the form.

    Code:
    Global txt As String
    
    Sub text()
    
    txt = Sheets("Sheet4").Cells(i, "B").Text & vbNewLine & _
    Sheets("Sheet4").Cells(i, "C").Text & vbNewLine & _
    Sheets("Sheet4").Cells(i, "D").Text & vbNewLine & _
    Sheets("Sheet4").Cells(i, "E").Text & vbNewLine & _
    Sheets("Sheet4").Cells(i, "F").Text & vbNewLine & _
    Sheets("Sheet4").Cells(i, "H").Text & vbNewLine & vbNewLine
    
    UserForm1.Show
    
    End Sub
    The problem is: The form shows up with the text box, but no text in it. When I click inside and type any key. The text shows up. Why does this happen? and how do i make the text show up right away on load?

    Thanks

  2. #2
    Addicted Member
    Join Date
    Jan 2009
    Posts
    183

    Re: User form textbox issue

    Probably because you put the code in the textbox's change event. How exactly are you sending the text to the form?

    I'd add a public property to the form (e.g. dialogText) and use something like this to set the text:

    Public Property Let dialogText(ByVal value As String)
    me.TextBox1.text = value
    End Property


    Or if you are using a global for txt, then put the code in the initialize event of the form:

    Private Sub UserForm_Initialize()
    me.TextBox1.text = txt
    End Sub
    Last edited by MarkWalsh; Jan 24th, 2011 at 04:00 PM.

  3. #3

    Thread Starter
    Junior Member
    Join Date
    Jan 2010
    Posts
    21

    Re: User form textbox issue

    I'm sending the text in in the vba code of the user form. When I right click on the form, one of the options is vba code.

    As in my example. I added the line TextBox1.Text = txt:

    Private Sub TextBox1_Change()

    TextBox1.Text = txt

    End Sub

  4. #4
    Addicted Member
    Join Date
    Jan 2009
    Posts
    183

    Re: User form textbox issue

    Quote Originally Posted by kavkazi View Post
    I'm sending the text in in the vba code of the user form. When I right click on the form, one of the options is vba code.

    As in my example. I added the line TextBox1.Text = txt:

    Private Sub TextBox1_Change()

    TextBox1.Text = txt

    End Sub
    But you are using the 'Change' event of the textbox, which only fires when the value changes. Hence, you only see the text when you start typing in the field (since that is the only time you are actually changing it's contents).

    If you put the code in the initialize event for the form, it will set the text when the form loads. Try replacing your code with this:
    Private Sub UserForm_Initialize()
    me.TextBox1.text = txt
    End Sub

  5. #5

    Thread Starter
    Junior Member
    Join Date
    Jan 2010
    Posts
    21

    Re: User form textbox issue

    perfect .. works great.. thx

  6. #6
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    Re: User form textbox issue

    in fact you do not need a global variable at all for this
    vb Code:
    1. Sub text()
    2. dim txt as string
    3. txt = Sheets("Sheet4").Cells(i, "B").Text & vbNewLine & _
    4. Sheets("Sheet4").Cells(i, "C").Text & vbNewLine & _
    5. Sheets("Sheet4").Cells(i, "D").Text & vbNewLine & _
    6. Sheets("Sheet4").Cells(i, "E").Text & vbNewLine & _
    7. Sheets("Sheet4").Cells(i, "F").Text & vbNewLine & _
    8. Sheets("Sheet4").Cells(i, "H").Text & vbNewLine & vbNewLine
    9. userform1.textbox1 = txt
    10. UserForm1.Show
    11.  
    12. End Sub
    i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
    Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next

    dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part

    come back and mark your original post as resolved if your problem is fixed
    pete

Posting Permissions

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



Click Here to Expand Forum to Full Width