PDA

View Full Version : Max length of textbox with multiline property.



sujittalukde
04-21-2008, 01:22 AM
I have a text box on a user form. I want to set the max length of the textbox to 45 characters and want to dispaly on label the remaining characters. ie if a user enters a number/word/space then label eill show 44 and so on. When the 45 characters will reach, the cursor should not move and the label ahould show 0
The multiline property of the textbox is set to true.
How can this be done?

dominicb
04-21-2008, 01:47 AM
Good morning sujittalukde

In the textbox properties set the property "MaxLength" to 45. Then use the code below. This assumes that your textbox is called TextBox1 and your label, Label1 :

Private Sub TextBox1_Change()
Label1.Caption = 45 - Len(TextBox1)
End Sub

Private Sub UserForm_Initialize()
' This line needs integrating into your initialise routine - if you have one
Label1.Caption = 45
End Sub

HTH

DominicB

tstav
04-21-2008, 02:05 AM
One more option (not tested).
Setting the maxLength of the textbox e.g. to 15, will allow the user
to write 15 characters and no more.
If the user wants to backtrack a few characters and insert a character he has forgotten,
(e.g. he wrote '58,Hde Park Ave' instead of 'Hide'), the maxLength will not allow any insertion.
So, the following might be a more flexible alternative.
Private sub textbox1_change()
If len(textbox1.text)<46 then
label1.caption=45-len(textbox1.text)
else
textbox1.text=left(textbox1.text,45)
'label1.caption=0
end if
end sub

Edit: And the label1.caption=0 seems unnecessary.

sujittalukde
04-21-2008, 02:23 AM
Thanks its working great. Can the text be aligned as Justified? The propertoes allows only Textalignment for Left,Right and centre.