Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Unbound Text Box - Limit To 255 Characters 3

Status
Not open for further replies.

ProjectExplorer

Programmer
Mar 22, 2002
95
GB
Is there a way to limit the number of characters in an unbound text box to 255 characters? - i.e. so that when the user reaches 255 characters they can no longer type any more into the text box.

At present the user can keep typing blisfully unaware that only the first 255 characters will actuall be saved.

Any help appreciated.

Thanks
 
You can check the Text property in the Change event.
 
Example:
Private Sub Text0_Change()
If Len(Me!Text0.Text) > 10 Then MsgBox ("cannot be more then 10")
End Sub
 
you could also set the bound field size in the table to 255.


Ian Mayor (UK)
Program Error
Always make your words short and sweet. Because you never know when you may have to eat them.
 
you could also set the bound field size in the table to 255

The OP clearly stated an unbound textbox.

fneily's suggestion is the way to go. If the OP wanted to, a label with the caption set to

255-Len(Me!Text0.Text)

could be placed next to the textbox to keep the user aware of how many characters remained that could be entered.


The Missinglinq

Richmond, Virginia

There's ALWAYS more than one way to skin a cat!
 
missingling you are correct. I misread the thread. getting late here 11pm.

If Len(Me!Text0.Text) > 10 Then MsgBox ("cannot be more then 10")

Although this would not limit the number of characters only notify the user they had exceeded the maximum number allowed.

so that when the user reaches 255 characters they can no longer type any more into the text box.

how about

Private Sub Text0_Change()
If Len(Me!Text0.Text) > 10
Me!Text0.Text = left(Me!Text0.Text,10)
MsgBox ("cannot be more then 10")
End Sub

Ian Mayor (UK)
Program Error
Always make your words short and sweet. Because you never know when you may have to eat them.
 
Another way:

Code:
Private Sub txtText_KeyPress(KeyAscii As Integer)
    If Me.txtText.SelStart >= 254 Then
        KeyAscii = 0
    End If
End Sub
 
Thanks for all your help - Remou, your code works a treat - just what I needed (I had never tried the SelStart property before). In the end I used the KeyPress event to limit the number of characters and the KeyUp event to inform the user how many characters have been used up(see below if any one needs it).

Private Sub txtContactNotes_KeyPress(KeyAscii As Integer)

'Limit the number of characters in the notes box to 255
If Me.txtContactNotes.SelStart > 254 Then
KeyAscii = 0
End If

End Sub

Private Sub txtContactNotes_KeyUp(KeyCode As Integer, Shift As Integer)

'Tell the user how many characters have been used
Me.lblCharacterCount.Caption = Nz(Len(Me.txtContactNotes.Text), 0)

End Sub
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top