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 TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Limiting the number of Characters in an unbound text box 1

Status
Not open for further replies.

WallT

Vendor
Aug 13, 2002
247
US
I have a form that has an unbound text box on it that I want to limit the number of characters to. I want it to just stop at 310 characters and not let anymore be added. Can I get some help please?
 
You can use the on change event to get the length of .text:

[tt]If Len(Trim(Me.txtTextbox.Text))=310 Then[/tt]
 
Hi, How about this:

Private Sub txtType_AfterUpdate()

If Len(Me.txtType) > 310 Then
MsgBox "Total Length Exceeds 310 Characters. Limiting text to 310 characters.", vbOKOnly
End If

Me.txtType = Left(txtType, 310)'Strip Extra Characters

Me.txtType.SetFocus


End Sub
 
Thank you both. I was on the same track as you suggest Remou, however, how do I stop the text from being entered?

Then ???

I would like to avoid the afterUpdate way only because I don't want somebody to type on and on not realizing it's going to get trimmed.
 
This is better, I think:

Code:
Private Sub txtText_KeyPress(KeyAscii As Integer)
    If Len(txtText.Text) >= 10 Then
        KeyAscii = 0
    End If
End Sub
 
It giving me an error on KeyAscil as "Variable not defined
 
Woops...spelling error...works great now...thanks for the help.
 
The above is the KeyPress event, not the Change event.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top