|
-
Jan 24th, 2011, 03:42 PM
#1
Thread Starter
Junior Member
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
-
Jan 24th, 2011, 03:56 PM
#2
Addicted Member
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.
-
Jan 24th, 2011, 04:02 PM
#3
Thread Starter
Junior Member
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
-
Jan 24th, 2011, 04:07 PM
#4
Addicted Member
Re: User form textbox issue
 Originally Posted by kavkazi
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
-
Jan 24th, 2011, 04:20 PM
#5
Thread Starter
Junior Member
Re: User form textbox issue
perfect .. works great.. thx
-
Jan 25th, 2011, 04:18 AM
#6
Re: User form textbox issue
in fact you do not need a global variable at all for this
vb Code:
Sub text() dim txt as string 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.textbox1 = txt UserForm1.Show 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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|