Is there any way to limit a TextBox to only 4 lines of text? I know I can
limit the number of characters, but users will be entering a mix of capital
and lower case letters, so it is hard to know what to set the max length to.
My TextBox is set up so that a text line can not exceed the length of the
line the text will be placed on.

Re: TextBox limit by Jay

Jay
Mon Apr 16 07:22:09 CDT 2007

On Sun, 15 Apr 2007 21:16:49 -0700, "Patrick C. Simonds"
<ordnance1@comcast.net> wrote:

>Is there any way to limit a TextBox to only 4 lines of text? I know I can
>limit the number of characters, but users will be entering a mix of capital
>and lower case letters, so it is hard to know what to set the max length to.
>My TextBox is set up so that a text line can not exceed the length of the
>line the text will be placed on.
>

No, that's not possible with a text form field. The best you can do is
to put the form field into a table cell that has been formatted with
fixed column width and an exact row height. That doesn't prevent users
from pressing Enter or otherwise inserting more characters, but the
extra won't be visible. If you use a macro to retrieve the content of
the form field, though, all the extra text will be there.

If you really must constrain the amount of text, get rid of the form
fields and use a UserForm instead
(http://www.word.mvps.org/FAQs/Userforms/CreateAUserForm.htm). The
text box controls in a UserForm allow you to examine their contents on
every keystroke (by writing code in the control's Change procedure)
and take action if the text is too long, contains invalid characters,
etc.

One bit of advice: When asking about text form fields, don't call them
text boxes -- that's something completely different (on the menu,
Insert > Text Box).

--
Regards,
Jay Freedman
Microsoft Word MVP FAQ: http://word.mvps.org
Email cannot be acknowledged; please post all follow-ups to the
newsgroup so all may benefit.

Re: TextBox limit by Patrick

Patrick
Mon Apr 16 09:06:30 CDT 2007

Sorry, I was not clear enough. I am talking about a TextBox on a UserForm.


"Jay Freedman" <jay.freedman@verizon.net> wrote in message
news:p7q623d6c0ovp3f7c289hktgualvifdqur@4ax.com...
> On Sun, 15 Apr 2007 21:16:49 -0700, "Patrick C. Simonds"
> <ordnance1@comcast.net> wrote:
>
>>Is there any way to limit a TextBox to only 4 lines of text? I know I can
>>limit the number of characters, but users will be entering a mix of
>>capital
>>and lower case letters, so it is hard to know what to set the max length
>>to.
>>My TextBox is set up so that a text line can not exceed the length of the
>>line the text will be placed on.
>>
>
> No, that's not possible with a text form field. The best you can do is
> to put the form field into a table cell that has been formatted with
> fixed column width and an exact row height. That doesn't prevent users
> from pressing Enter or otherwise inserting more characters, but the
> extra won't be visible. If you use a macro to retrieve the content of
> the form field, though, all the extra text will be there.
>
> If you really must constrain the amount of text, get rid of the form
> fields and use a UserForm instead
> (http://www.word.mvps.org/FAQs/Userforms/CreateAUserForm.htm). The
> text box controls in a UserForm allow you to examine their contents on
> every keystroke (by writing code in the control's Change procedure)
> and take action if the text is too long, contains invalid characters,
> etc.
>
> One bit of advice: When asking about text form fields, don't call them
> text boxes -- that's something completely different (on the menu,
> Insert > Text Box).
>
> --
> Regards,
> Jay Freedman
> Microsoft Word MVP FAQ: http://word.mvps.org
> Email cannot be acknowledged; please post all follow-ups to the
> newsgroup so all may benefit.



Re: TextBox limit by Jay

Jay
Mon Apr 16 21:41:03 CDT 2007

OK, that is the important distinction.

It looks like this will do what you asked:

Private Sub TextBox1_Change()
Dim temp As String
If TextBox1.CurLine > 3 Then ' 0-based
MsgBox "Input can't be more than 4 lines."
temp = TextBox1.Text
If Right$(temp, 2) = vbCrLf Then
temp = Left$(temp, Len(temp) - 2)
Else
temp = Left$(temp, Len(temp) - 1)
End If
TextBox1.Text = temp
End If
End Sub

It doesn't matter whether the line breaks in the box occur because of
pressing Enter or because of automatic line wrapping.

--
Regards,
Jay Freedman
Microsoft Word MVP FAQ: http://word.mvps.org
Email cannot be acknowledged; please post all follow-ups to the
newsgroup so all may benefit.

On Mon, 16 Apr 2007 07:06:30 -0700, "Patrick C. Simonds"
<ordnance1@comcast.net> wrote:

>Sorry, I was not clear enough. I am talking about a TextBox on a UserForm.
>
>
>"Jay Freedman" <jay.freedman@verizon.net> wrote in message
>news:p7q623d6c0ovp3f7c289hktgualvifdqur@4ax.com...
>> On Sun, 15 Apr 2007 21:16:49 -0700, "Patrick C. Simonds"
>> <ordnance1@comcast.net> wrote:
>>
>>>Is there any way to limit a TextBox to only 4 lines of text? I know I can
>>>limit the number of characters, but users will be entering a mix of
>>>capital
>>>and lower case letters, so it is hard to know what to set the max length
>>>to.
>>>My TextBox is set up so that a text line can not exceed the length of the
>>>line the text will be placed on.
>>>
>>
>> No, that's not possible with a text form field. The best you can do is
>> to put the form field into a table cell that has been formatted with
>> fixed column width and an exact row height. That doesn't prevent users
>> from pressing Enter or otherwise inserting more characters, but the
>> extra won't be visible. If you use a macro to retrieve the content of
>> the form field, though, all the extra text will be there.
>>
>> If you really must constrain the amount of text, get rid of the form
>> fields and use a UserForm instead
>> (http://www.word.mvps.org/FAQs/Userforms/CreateAUserForm.htm). The
>> text box controls in a UserForm allow you to examine their contents on
>> every keystroke (by writing code in the control's Change procedure)
>> and take action if the text is too long, contains invalid characters,
>> etc.
>>
>> One bit of advice: When asking about text form fields, don't call them
>> text boxes -- that's something completely different (on the menu,
>> Insert > Text Box).

Re: TextBox limit by Patrick

Patrick
Mon Apr 16 23:09:49 CDT 2007

Thank you very much, that did the trick.

"Jay Freedman" <jay.freedman@verizon.net> wrote in message
news:ljb823pdhcagl5q9q3u59jfejmcfuiblnj@4ax.com...
> OK, that is the important distinction.
>
> It looks like this will do what you asked:
>
> Private Sub TextBox1_Change()
> Dim temp As String
> If TextBox1.CurLine > 3 Then ' 0-based
> MsgBox "Input can't be more than 4 lines."
> temp = TextBox1.Text
> If Right$(temp, 2) = vbCrLf Then
> temp = Left$(temp, Len(temp) - 2)
> Else
> temp = Left$(temp, Len(temp) - 1)
> End If
> TextBox1.Text = temp
> End If
> End Sub
>
> It doesn't matter whether the line breaks in the box occur because of
> pressing Enter or because of automatic line wrapping.
>
> --
> Regards,
> Jay Freedman
> Microsoft Word MVP FAQ: http://word.mvps.org
> Email cannot be acknowledged; please post all follow-ups to the
> newsgroup so all may benefit.
>
> On Mon, 16 Apr 2007 07:06:30 -0700, "Patrick C. Simonds"
> <ordnance1@comcast.net> wrote:
>
>>Sorry, I was not clear enough. I am talking about a TextBox on a UserForm.
>>
>>
>>"Jay Freedman" <jay.freedman@verizon.net> wrote in message
>>news:p7q623d6c0ovp3f7c289hktgualvifdqur@4ax.com...
>>> On Sun, 15 Apr 2007 21:16:49 -0700, "Patrick C. Simonds"
>>> <ordnance1@comcast.net> wrote:
>>>
>>>>Is there any way to limit a TextBox to only 4 lines of text? I know I
>>>>can
>>>>limit the number of characters, but users will be entering a mix of
>>>>capital
>>>>and lower case letters, so it is hard to know what to set the max length
>>>>to.
>>>>My TextBox is set up so that a text line can not exceed the length of
>>>>the
>>>>line the text will be placed on.
>>>>
>>>
>>> No, that's not possible with a text form field. The best you can do is
>>> to put the form field into a table cell that has been formatted with
>>> fixed column width and an exact row height. That doesn't prevent users
>>> from pressing Enter or otherwise inserting more characters, but the
>>> extra won't be visible. If you use a macro to retrieve the content of
>>> the form field, though, all the extra text will be there.
>>>
>>> If you really must constrain the amount of text, get rid of the form
>>> fields and use a UserForm instead
>>> (http://www.word.mvps.org/FAQs/Userforms/CreateAUserForm.htm). The
>>> text box controls in a UserForm allow you to examine their contents on
>>> every keystroke (by writing code in the control's Change procedure)
>>> and take action if the text is too long, contains invalid characters,
>>> etc.
>>>
>>> One bit of advice: When asking about text form fields, don't call them
>>> text boxes -- that's something completely different (on the menu,
>>> Insert > Text Box).