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

limit text len for unbound control 2

Status
Not open for further replies.

ecannelora

Programmer
Feb 4, 2005
34
US
I'm trying to limit the number of characters the user can enter into a text box. The limit should be 255.

considerations:

I don't want to use a validation rule because I don't want the validation text to pop up.

I don't want to use an input mask because it makes typing very messy.

I have a counter to tell the user how many characters he/she has remaining. Code for that is:

Private Sub txtNotes_Change()
Me.txtCharsUsed = 255 - Len(Me.txtNotes.Text)
End Sub

What I'd like to do is something like:
[in psuedo-code]
if len(control.text)>255 then
disallow additions
end if

any ideas?

 
how about,
Private Sub txtNotes_KeyPress(KeyAscii As Integer)
'or _Change()
if len(txtNotes.text) > 255 then
SendKeys ("{BS}")
end if
end sub
 
You may try this:
Private Sub txtNotes_KeyPress(KeyAscii As Integer)
If Len(txtNotes.Text) >= 255 Then
KeyAscii = 0
End If
End Sub

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ222-2244
 
Thanks a lot, guys! the SendKeys("{BS}") works, but it goes back 2

The KeyAscii=0 does exactly what I wanted it do do. Thanks a lot, PHV

Do you know where I can get a complete list of Ascii codes, and what keys they represent?
 
one other thing- how can I still allow the back space?
 
Private Sub txtNotes_KeyPress(KeyAscii As Integer)
If Len(txtNotes.Text) >= 255 And KeyAscii >= 32 Then
KeyAscii = 0
End If
End Sub

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ222-2244
 
never mind:

If Len(Me.txtNotes.Text) > 254 Then
If Not KeyAscii = 8 Then
KeyAscii = 0
End If
End If

perfect

thanks again, PHV and gecko101
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top